Jump to content

Make NULL value from empty date formfield


eurob

Recommended Posts

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

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?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.