开发者

Set date to NULL in MySQL database when leaving date field empty

hi have a HTML 5 date field for release date but not all items has a release date so I want to be able to set it to null and hide releasedate entirely for those items. When I leave the date field empty nothing happens. The previous date (2011-01-01) is still there (I thought they had a release date but i didn'开发者_JAVA技巧t know what it was so I just picked one sinced the table didn't allow null before). I have changed it to allow null now and I have managed to make it null in phpMyAdmin but not by using my own forms and php.

Any ideas?


There are two main ways.

The first, is to NOT insert/update the date when it's empty, by leaving it out of the sql entirely. You can build your prepared statement looping on dates values.

The second method is to encapsulate the date value into a function, which check if it's empty. The trick is that you usually wrap a date around (double) quotes, but you can't do that if you want to insert null (and not the string "null")

Personally I use a pre-pdo class I wrote to prepare the sql and the data before sending any sql to the database.

There might be tings already integrated precisely for that in pdo or mysqli, but I don't know them.

to your question: "what will be the result of null date value?" i invite you to read that old answer


Leave the date field NULL

INSERT INTO table SET dt=NULL
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜