Jump to content

Date problem


jeff5656

Recommended Posts

I have a form with a date.

When I put it into the datatbase I use this:

 

$dob = date("Y-m-d", strtotime($_POST['dob']));

 

However, if the user leaves that blank, it is stored as 0000-00-00 instead of just remaining blank.

I tried changing from NOT NULL to NULL in the table, but that had no effect.

Other than changing the date filed to VARCHAR (not ideal obviously), how do you keep that field blank if it is truly blank?

 

Link to comment
Share on other sites

$dob = (isset($_POST['dob']) && !empty($_POST['dob'])) ? date("Y-m-d", strtotime($_POST['dob'])) : NULL;

 

Try that out and see if it works for you. (The ? : is called that Ternary operator if you want more information on how it works).

 

Link to comment
Share on other sites

Nope.  it still is storing it as 0000-00-00

 

Interesting, let me ask you this...why does it matter if it shows as "blank" or not?

 

I do believe the only other way to prevent this (which I do not know if it will work) is if you omit the date field from the insert query. However, you should check your table structure and make sure a default value has not been set for the datefield, as that will always put that value in when nothing else is present.

 

I will do some more digging to see if I can find anything difinitive, but I am pretty sure that is how MySQL is setup.

 

EDIT:

Well my digging yielded some interesting results:

If you are not using the NO_ZERO_DATE SQL mode, MySQL also allows you to store '0000-00-00' as a “dummy date.” This is in some cases more convenient than using NULL values.

 

Found at http://dev.mysql.com/doc/refman/5.0/en/using-date.html  might be some good reading as it does seem you can configure your MySQL to do nulls instead of the 0's.

Link to comment
Share on other sites

I read that but they didn't explain what do do if you do NOT want a dummy date.

If I go into phpmyadmin and there is a patient smith with dob as "0000-00-00" and I delete that and leave it blank, when I go back to Smith the dob field is back to 0000-00-00!

 

There was even a thread from someone with the same problem here:

 

http://www.phpfreaks.com/forums/index.php?topic=141792

 

and it was never solved and that was 2007.  But what do you guys do if a user leaves a field blank?  The database does not accept blank so it stores it as 0000-00-00!

 

Also, I changed the dob field to NULL instead of NOT NULL and it had no effect.

 

Link to comment
Share on other sites

Ok this is weird.  When I add a new DATE field in phpmyadmin nd I browse the records, they all have 0000-00-00 as the value!

Does anyone have any odea what is goin on here? If I add a new DATE field with NULL then ALL the records have "Null" as the value.

 

Check there isn't a DEFAULT flag on that field.  This query will set the default back to 'NULL' again (obviously, put in your table name and column name).

 

ALTER TABLE your_table MODIFY date_column DATE DEFAULT NULL;

Link to comment
Share on other sites

I tried changing null and not null.  In both cases a blank is not allowed.  If not null, the computer adds 0000-00-00 if the form was blank.  If it's null it puts in "null".

 

So basically what I'm asking is, how do I stop the database from putting in a value (000-000-00) when the POST value was BLANK?  I am gathering from lack of responses that this is not possible?  Does that mean you guys don't have any blank DATE fields??

Link to comment
Share on other sites

Just a couple of things I've noticed that may have some bearing on this:

[*]I have trouble with mysqladmin assigning a DEFAULT clause when creating a table with NOT NULL columns.  Or removing the default from a column so that there is NO default (seems mysql wants a default if the column is not null, but it should NOT)

[*]I have trouble changing a value in the mysqlbrowser to NULL.

[*]When you are adding $dob to the query string, how are you doing it?

 

I'm at work and don't have access to an environment to check things on, so this is all from memory.

 

// Example 1
$dob = (empty($_POST['dob']) ? 'NULL' : $_POST['dob']);
$sql = "UPDATE myTable SET myDOB = '" . $dob . "' WHERE myID = " . $id . "'";

// Example 2
$dob = (empty($_POST['dob']) ? NULL : date($_POST['dob']));
$sql = "UPDATE myTable SET myDOB = '" . $dob . "' WHERE myID = " . $id . "'";

// Example 3
$sql = "UPDATE myTable SET myDOB = " .  
  (empty($_POST['dob']) ? 'NULL' : "'" . date($_POST['dob']) . "'") .
  . " WHERE myID = " . $id . "'";

Ex 1-- is going to put the string 'NULL' into a date field, which is illegal and results in a "zero date". 

Ex 2-- is going to put an empty string in there, also illegal

Ex 3--should put a NULL in the field since the resulting query will not have quotes around the word NULL.

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.