bsanderson Posted March 3, 2006 Share Posted March 3, 2006 I have a date field in a MySQL database which is set to accept NULLs. If I enter a record from phpMyAdmin and leave the date field blank, the record is entered properly. However, if I do the same thing from a form, I get an error that the date cannot be blank. It seems that I need to enter a NULL in the date field from the form rather that nothing. At the suggestion of someone on a newsgroup, I added the following line:if ($date === '' or !isset($date)) $date='NULL';This almost works, but the resultant statement contains the single quotation marks, which cause an error. INSERT INTO jobs ( JobNumber, DateRcvd, DateEff ) VALUES ( '9999', 'NULL', 'NULL')Does not work, but INSERT INTO jobs ( JobNumber, DateRcvd, DateEff ) VALUES ( '9999', NULL, NULL)does.This is my actual query:$query = "INSERT INTO jobs ( JobNumber, DateRcvd, DateEff, ) VALUES ( '$JobNumber','$DateRcvd','$DateEff',)";If I remove the single quotes from the date values, the nulls are entered correctly but that only works if the field is null. If a date is entered, it doesn't work. Ahy help will be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/4005-cant-pass-null-date-from-form/ Share on other sites More sharing options...
yonta Posted March 3, 2006 Share Posted March 3, 2006 Well, maybe you could vary your query so if (DateRcvd == "")query = ...elseif(DateEff == "")query=..elsequery =..Probably not the best approach but the only one i could think of now reading your post.Sofia Quote Link to comment https://forums.phpfreaks.com/topic/4005-cant-pass-null-date-from-form/#findComment-13949 Share on other sites More sharing options...
fenway Posted March 3, 2006 Share Posted March 3, 2006 The easiest solution is simply to write your query as follows:[code]$query = "INSERT INTO jobs ( JobNumber, DateRcvd, DateEff, ) VALUES ( '$JobNumber',$DateRcvd,$DateEff)";[/code]And have PHP add the single quotes if necessary:[code]$DateRcvd = ($DateRcvd === '' or !isset($DateRcvd)) ? "NULL" : "'$DateRcvd'";[/code]Do the same for $DateEff, if necessary.Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/4005-cant-pass-null-date-from-form/#findComment-13977 Share on other sites More sharing options...
bsanderson Posted March 6, 2006 Author Share Posted March 6, 2006 That did it. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/4005-cant-pass-null-date-from-form/#findComment-14623 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.