You still can’t do it that way.
You either need to have code like this:
INSERT INTO Table1 (field1, field2, field3) VALUES ('1', NULL, '3')
or you need to have something like this:
INSERT INTO Table1 (field1, field3) VALUES ('1', '3')
Your code would give option 3, which doesn’t work…
INSERT INTO Table1 (field1, field2, field3) VALUES ('1', , '3')
This is an ULTRA dumbed down version (no sql injection protection, should be using parameterized queries, etc) but essentially will give option #2 above.
echo '<pre>';print_r($_POST);echo '</pre>';
$insertPortion = "INSERT INTO utilities(";
$valuesPortion = ") VALUES (";
if(!empty($_POST['Name'])) {
$insertPortion .= "name, ";
$valuesPortion .= "'" . $_POST['Name'] . "', ";
}
if(!empty($_POST['Type'])) {
$insertPortion .= "type, ";
$valuesPortion .= "'" . $_POST['Type'] . "', ";
}
if(!empty($_POST['Capacity'])) {
$insertPortion .= "capacity, ";
$valuesPortion .= "'" . $_POST['Capacity'] . "', ";
}
if(!empty($_POST['Serving'])) {
$insertPortion .= "serving, ";
$valuesPortion .= "'" . $_POST['Serving'] . "', ";
}
if(!empty($_POST['Circuit'])) {
$insertPortion .= "circuit, ";
$valuesPortion .= $_POST['Circuit'] . ", ";
}
if(!empty($_POST['meter_load'])) {
$insertPortion .= "meter_load, ";
$valuesPortion .= $_POST['meter_load'] . ", ";
}
if(!empty($_POST['Voltage'])) {
$insertPortion .= "voltage, ";
$valuesPortion .= $_POST['Voltage'] . ", ";
}
if(!empty($_POST['Frequency'])) {
$insertPortion .= "frequency, ";
$valuesPortion .= $_POST['Frequency'] . ", ";
}
if(!empty($_SESSION['email'])) {
$insertPortion .= "created_by, ";
$valuesPortion .= "'" . $_SESSION['email'] . "', ";
}
$insertPortion .= "created_date, ";
$valuesPortion .= "'CURRENT_TIMESTAMP, ";
if(!empty($_POST['Notes'])) {
$insertPortion .= "notes, ";
$valuesPortion .= "'" . $_POST['Notes'] . "', ";
}
if(!empty($_POST['enabled'])) {
$insertPortion .= "enabled, ";
$valuesPortion .= $_POST['enabled'] . ", ";
}
$sql = substring($insertPortion, 0, -3) . substring($valuesPortion, 0, -3) . ")"