PHP - Insert date into mysql
119,146
Solution 1
try CAST
function in MySQL:
mysql_query("INSERT INTO data_table (title, date_of_event)
VALUES('". $_POST['post_title'] ."',
CAST('". $date ."' AS DATE))") or die(mysql_error());
Solution 2
try
$date = "2012-08-06";
$date=date("Y-m-d",strtotime($date));
Solution 3
Unless you want to insert different dates than "today", you can use CURDATE():
$sql = 'INSERT INTO data_tables (title, date_of_event) VALUES ("%s", CURDATE())';
$sql = sprintf ($sql, $_POST['post_title']);
PS! Please do not forget to sanitize your MySQL input, especially via mysql_real_escape_string ()
Author by
xperator
I like this quote the most and I'd love to live up to it : "If you don't build your own dream, Someone else will hire you to build their"
Updated on July 05, 2022Comments
-
xperator almost 2 years
I am trying to insert date into mysql but everytime it fails and comes out as
0000-00-00
in phpmyadminMy date format is like
2012-08-06
(yyyy-mm-dd) and the date field type in database isdate
.$date = "2012-08-06"; mysql_query("INSERT INTO data_table (title, date_of_event) VALUES('". $_POST['post_title'] ."', '". $date ."')") or die(mysql_error());
tried changing
-
to/
or removing them, it doesn't work.