Insert NULL into date field if form var is empty ARGH

This is driving me crazy. It WORKS for one field, but not the other:

[php] $spouseb = trim($_POST[‘spouse_birthday’]);
if($spouseb != ‘’) {
$sbirth = “’”.date(“Y-m-d”,strtotime(str_replace(’-’,’/’,$spouseb)))."’"; }
else { $sbirth = “NULL”; }

 $memb = trim($_POST['birthday']);
	if($memb != '') {
       $mbirth = "'".date("Y-m-d",strtotime(str_replace('-','/',$memb)))."'"; }
	else { $mbirth = "NULL"; }  [/php]

Then, my update query is simply:

spouse_birthday = $sbirth, birthday = $mbirth,

So, the $sbirth block works. If no date is entered into the form, the query inserts a NULL value.
$mbirth block? I get: 12/31/1969 and not NULL

If I enter actual dates and not leave the fields blank, the dates are formatted and inserted correctly.

In the database, both spouse_birthday and birthday are date fields, with no default value set. Nulls are allowed.

As a test, I can delete all the form vars and date formatting and just set the two variables to NULL
[php]
$sbirth = “NULL”;
$mbirth = “NULL”;[/php]

Now, I KNOW my values are null. The update STILL inserts 12/31/1969 for $mbirth, and NULL for $sbirth.

Ideas? Before I pull ALL my hair out? Why will it insert NULL for one, but not the other?

OK, went completely bald before this got sorted!! I stripped my code/queries down to almost nothing BUT the date fields. Problem persisted. WTH??

Finally called hosting and asked them to check the database and run a maintenance cycle (compact/repair or whatever it is you do to MySQL). Was told “something doesn’t look right” after looking at it before running maintenance. Problem went away immediately after.

Sheesh, huh?

Have you tried changing it to NULL without speech marks?

Note to self… read full post.

Sponsor our Newsletter | Privacy Policy | Terms of Service