Hello, Hello.
I'm having a hell of a time (probably due to lack of sleep and deminished brain capacity, not that I had some to begin with) inserting null values. For example, not all fields are required.
lname VARCHAR(25) = last name (text) = required
fname VARCHAR(25) = first name (text) = required
hospital INT(11) = hospital (hospital_id linked to other table) = required
room VARCHAR(10) = room number (text or num) = not required
gender VARCHAR(1) = gender (text: m, f) = not required
Any help is greatly appreciated, I can provide more information as well at any time. THANK YOU ALL AGAIN! I LOVE THIS PLACE!
//Retrieve variables
$ar_fields = array('lname', 'fname', 'hospital', 'room', 'gender');
foreach($_POST as $key => $value) {
if (in_array($key, $ar_fields)) {
//echo $key, ": ", $value, "<br>";
//If $value is empty then the text substituted will be NULL, otherwise $value.
//$value = ($value ? $value : "NULL");
$value = ($value ? "'".mysql_real_escape_string($value)."'" : 'NULL');
$$key = trim($value); //i.e: $name = Manny (trim whitespace before and after)
}
} // end foreach
/* if(!isset($gender)) { $gender = NULL; }*/
//if no data submitted, display error and retry
if ($lname=='' || $fname=='' || $hospital=='') {
header("LOCATION: patient.php?q=error");
exit;
} else {
//First Insert New Patient Record
/* THIS IS THE INSANE MANUAL WAY OF DOING IT, WORKS!
$patientSQL = "INSERT INTO patients
(lname, fname, room, gender, hospital_id)
VALUES
('$lname', '$fname', ";
$patientSQL .= empty($room) ? "NULL" : $room;
$patientSQL .= ", '$gender', '$hospital')."')";*/
/*THIS IS SO CLOSE BUT THE DATA DOES NOT GET INPUTTED INTO MYSQL*/
$patientSQL = "INSERT INTO patients
(lname, fname, room, gender, hospital_id)
VALUES
($lname, $fname, $room, $gender, $hospital)";
echo $patientSQL.'<br>';
mysql_query($patientSQL); // execute sql