PHP - 将日期插入 mysql

2022-01-09 00:00:00 format date insert php mysql

我正在尝试将日期插入 mysql,但每次失败并在 phpmyadmin 中显示为 0000-00-00

I am trying to insert date into mysql but everytime it fails and comes out as 0000-00-00 in phpmyadmin

我的日期格式是 2012-08-06 (yyyy-mm-dd),数据库中的日期字段类型是 date.

My date format is like 2012-08-06 (yyyy-mm-dd) and the date field type in database is date.

$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.

推荐答案

在 MySQL 中尝试 CAST 函数:

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()); 

相关文章