如何在带有bind_param的MySQLi中插入带有MySQL NOW()函数的记录


How to insert the record with MySQL NOW() function in MySQLi with bind_param?

我需要在MySQLi PDO中用PHP中的bind-parameter执行这个mysql查询:

mysql_query("INSERT INTO `posts` (post_name,publish_date) VALUES ($post_name,NOW()) ")

我使用这样的脚本,但它没有正确插入publish_date。

$publish_date = 'NOW()';
$insert = $mysqli->prepare("INSERT INTO posts (post_name,publish_date) VALUES (?,?)");
$insert->bind_param("ss", $post_name $publish_date);
$insert->execute();

它将记录插入publish_date列,如下所示:0000-00-00 00:00:00

我该怎么做?提前谢谢。

p.S:日期列的类型为datatime

它不是查询的参数,因为您不必向MySQL提供值。

$insert = $mysqli->prepare("INSERT INTO posts (post_name, publish_date) VALUES (?, NOW())");

也许您应该尝试使用日期函数,而不是NOW()

$publish_date =date("Y-m-d H:i:s");
$insert = $mysqli->prepare("INSERT INTO posts (post_name,publish_date) VALUES (?,?)");
$insert->bind_param("ss", $post_name $publish_date);
$insert->execute();