eurob Posted January 10, 2009 Share Posted January 10, 2009 I have a table in mySql with a NULL datetime field. In my form the date field can be empty so I want to do an insert as this: insert into mytable(id,date) values($id,$date) However, when I leave the $date empty (on the form) php complains that $date has an invalid value. In mySql however, I can do insert into mytable(id,date) values(1,NULL) How can I make PHP recognize my $date field as a NULL value? Thanks Link to comment https://forums.phpfreaks.com/topic/140288-make-null-value-from-empty-date-formfield/ Share on other sites More sharing options...
Daniel0 Posted January 10, 2009 Share Posted January 10, 2009 "NULL" is a string, NULL isn't. You need to check if it's empty and set it to "NULL" yourself. Link to comment https://forums.phpfreaks.com/topic/140288-make-null-value-from-empty-date-formfield/#findComment-734048 Share on other sites More sharing options...
eurob Posted January 10, 2009 Author Share Posted January 10, 2009 I notice I have to write different sql statements because when I would do this: if (empty($date))$date=NULL; and then do this: insert into mytable(id,date) values($id,$date) it will still errors with 'incorrect datetime value of ". So then I have to do it like this if (empty($date))insert into mytable(id,date) values($id,NULL) Is there a handier way to do this? Link to comment https://forums.phpfreaks.com/topic/140288-make-null-value-from-empty-date-formfield/#findComment-734217 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.