0

On my form, I have a date field that is not a required field. My problem is if users don't fill in this field and they submit the form, on mysql db, instead of leaving the field blank, it will instead save 1969-12-31 and any succeeding form that has been submitted on the same day will have ascending dates from the 1969-12-31 if this field is once again left blank. What do I change so that mysql will simply leave that field blank. Thanks for reading.

Here are the codes:

<input id="nfud" name="nfud" type="date" class="input-small"/>

Mysql submit query

$nfud = date('Y-m-d', strtotime($_POST["nfud".$n.""]));
$query="insert into number_info (phone_number, data_plan, phone_used, fud, dealID) 
values ('".$nphone_number."','".$ndata_plan."','".$nphone_used."','".$nfud."','$dealID')";
        mysql_query($query) or die(mysql_error());
1
  • is date field set to not null?? Commented Dec 8, 2012 at 9:02

3 Answers 3

2

Just leave the field empty if you did not post a date from your form:

if(isset($_POST["nfud$n"]) {
    $nfud = date('Y-m-d', strtotime($_POST["nfud$n"]));
} else {
    $nfud = "";
}
$query="insert into number_info (phone_number, data_plan, phone_used, fud, dealID) 
values ('".$nphone_number."','".$ndata_plan."','".$nphone_used."', '".$nfud."','$dealID')";
        mysql_query($query) or die(mysql_error());

EDIT : Try this to let MySQL put a default value for your column

if(isset($_POST["nfud$n"]) {
    $nfud = date('Y-m-d', strtotime($_POST["nfud$n"]));
    $query="insert into number_info (phone_number, data_plan, phone_used, fud, dealID) 
            values ('".$nphone_number."','".$ndata_plan."','".$nphone_used."','".$nfud."','$dealID')";

} else {
    $query="insert into number_info (phone_number, data_plan, phone_used, dealID) 
            values ('".$nphone_number."','".$ndata_plan."','".$nphone_used."','$dealID')";
}

mysql_query($query) or die(mysql_error());
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the reply. I tried this, the only thing that change is now the date does not ascend but stuck on 1970-01-01. I even tried doing $nfud = "0000-00-00"; but it is still saving 1970-01-01. I also set default on the mysql date field as 0000-00-00
Try my edit but I'm not sure it works, is you field of type DATE or DATETIME? For DATETIME you cannot store NULL values
Nope, it didn't work. It is actually giving me a syntax error. The sql field is DATE.
Updated to insert the date only if there is a date, else let MySQL put a default value. Set DEFAULT NULL to your date column
Still didn't work. I've been reading around and it seems to have something to do with using strtotime
0

in mysql, give the date field a default value of 0.

1 Comment

Thanks for the reply. I set the mysql date field to default 0000-00-00 but still when the form is submitted 1970-01-01 is saved instead
0

This can be controlled on the mysql side with default and other settings.

Can you run the following in mysql and share the results?

SHOW CREATE TABLE number_info;

2 Comments

I ran it but I am not sure what results I should be showing. MySql is only saying CREATE TABLE `number_info` ( `numberID` int(11) ...
Yes, that is what it should show. Can you show the full output?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.