可能重复:
illegal 0000-00-00 00:00:00 timestamp being sent by php to mysql using codeigniter
我正在尝试使用日期/时间组合为查询添加时间戳,但是当查询运行时,它会将0000-00-00 00:00:00
加载到日期字段中。
查询:
$link = mysqli_connect("$server", "$user", "$pass", "$webdb");
$title = mysqli_real_escape_string($link, (string) $_POST['title']);
$content = mysqli_real_escape_string($link, (string) $_POST['content']);
$query = "INSERT INTO `blog` (`title`, `content`, `posted_by`, `published`, `profile_pic`, `date`)
VALUES ('$title', '$content','$display_name', '1', '$profile_pic', 'getdate()')";
mysqli_query($link, $query);
mysqli_close($link);
有谁看到我做错了什么?提前谢谢。
答案 0 :(得分:1)
'getdate()'
被解释为一个字符串,MySQL不理解。
使用NOW()
但没有引号。
即
$query = "INSERT INTO `blog` (`title`, `content`, `posted_by`, `published`, `profile_pic`, `date`)
VALUES ('$title', '$content','$display_name', '1', '$profile_pic', NOW())";
答案 1 :(得分:1)
使用NOW()
而不是在SQL查询中使用getdate()
...
答案 2 :(得分:1)
我认为getdate()
不被评估。它试图将字符串文字"getdate()"
插入表中,而不是管理,因此它会回退到0000-00-00 00:00:00
。
使用NOW()
(不带单引号)。