0

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'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?

Alex
  • 731
  • 1
  • 11
  • 24
  • You'll have to manually clean up the table to null out those fields where you filled in a bogus date. Unless there's some other way of picking out those records that you could write up a 'where' clause for on an update query, you're in for a full table edit to clean up the bogus values. – Marc B Sep 07 '11 at 15:48
  • Then what will happen if I enter a new post and it has no releasedate. Also what is the output of an empty date field? I want to write an if statement to set it to null. – Alex Sep 07 '11 at 16:00
  • Explicitly insert a null value, just in case db decides to assign a default value instead. And a db null (and regular empty string) will output an empty string in PHP. – Marc B Sep 07 '11 at 16:06

2 Answers2

1

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

Community
  • 1
  • 1
roselan
  • 3,755
  • 1
  • 20
  • 20
0

Leave the date field NULL

INSERT INTO table SET dt=NULL
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345