I am trying to enter a NULL value into a MySQL DB when the user does not set a date on a form input. For some reason even when I set the value to NULL before entering it, the value goes to '0000-00-00' in my DB. What I am trying to do is update a date field which had been previously set to a date and now I want to reset it to NULL.
I tried then entering a NULL value directly into my query string but I get an error because of the missing "'". If I did include the "'" around the NULL value then it sets the field to the '0000-00-00'. Unfortunately I cannot simply enter the values via prepared statements because I am creating an entire SQL statement as the prepared statement.
HTML:
<input name="datefield" type="date"/>
PHP:
while($row = $getfields->fetch(PDO::FETCH_ASSOC)) {
$temp_col = $row['FieldName'];
$field_value = $_POST[$temp_col];
if (!isset($field_value) || empty($field_value)) {
$field_value = NULL;
$update_array = $temp_col . "=" . $field_value; <-- PROBLEM HERE!
} else {
$update_array = $temp_col . "='" . $field_value . "'";
}
}
$updatefields=$db->prepare("UPDATE MyMainTable SET $update_array WHERE ItemNum = ?");
$updatefields->execute(array($personid));