3

I am trying to add current date to MySQL table using PHP. I have the following code:

$date = date('Y-m-d');
mysql_query("INSERT INTO book_order VALUES($date)")  or die (mysql_error());

MySQL returns this error:

Incorrect date value: '1996' for column 'date_order' at row 1

date_order attribute is type date.

I've tried to echo the $date value and prints out 2013-05-12

Why I can't add the date to the table and what is this strange 1996?

1

4 Answers 4

2

If you can send local date use:

mysql_query("INSERT INTO book_order VALUES(NOW())"); 

The your problem is because your SQL have an typo - $date variable must be quoted:

mysql_query("INSERT INTO book_order VALUES('$date')")  or die (mysql_error());

In addition, if this not work edit your question and paste the result from SQL EXPLAIN book_order

Sign up to request clarification or add additional context in comments.

1 Comment

or in the format:(yyyy-mm-dd) - mysql_query("INSERT INTO book_order VALUES(format(NOW(),'YYYY-MM-DD')");
2

Use STR_TO_DATE :

$date = date('Y-m-d');
mysql_query("INSERT INTO book_order VALUES (STR_TO_DATE('".$date ."','%Y-%m-%d'))");

1 Comment

Read my comment on Owen's answer. You have to wrap the date string in quotes (not only PHP's string quotes, but quotes in the query too). Else, your input will be interpreted as math expression like MattyF stated in his comment on the question.
1

tried

$date = date('Y-m-d');
mysql_query("INSERT INTO book_order VALUES('$date')")  or die (mysql_error());

????

1 Comment

Either escape the quotes in the query like \" (which is MySQL-spcific syntax) or use ' (which is standard SQL syntax). Can't understand the downvote, comment instead. Suggested an edit to correct the errors.
0

try this

$date = date('Y-m-d');
mysql_query("INSERT INTO book_order VALUES('$date')")  or die (mysql_error());

Comments

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.