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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.