OutOfBoundsException
OutOfBoundsException

Reputation: 766

Add date to MySQL using PHP

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?

Upvotes: 3

Views: 5529

Answers (4)

Ragen Dazs
Ragen Dazs

Reputation: 2170

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

Upvotes: 2

Siamak Motlagh
Siamak Motlagh

Reputation: 5136

Use STR_TO_DATE :

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

Upvotes: 2

owenmelbz
owenmelbz

Reputation: 6564

tried

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

????

Upvotes: 1

Nwafor
Nwafor

Reputation: 184

try this

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

Upvotes: 0

Related Questions