php 如何使用bind_param在MySQLi中使用MySQL NOW()函数插入记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9497186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:57:44  来源:igfitidea点击:

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

phpmysqlmysqlisqlbindparameter

提问by JohnUS

I need to execute this mysql query in MySQLi PDO with bind parametr in PHP:

我需要在 MySQLi PDO 中使用 PHP 中的绑定参数执行这个 mysql 查询:

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

I use the script like this, but it doesn't insert publish_date correctly.

我使用这样的脚本,但它没有正确插入 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();

It inserts the record into the publish_datecolumn like this: 0000-00-00 00:00:00

How can I do this ? Thanks in advance.

它会publish_date像这样将记录插入到列中:0000-00-00 00:00:00

我该怎么做?提前致谢。

P.S: The type of date column is datatime.

PS:日期列的类型是datatime.

回答by Hammerite

It's not a parameter of the query, in that you don't have to supply a value to MySQL.

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

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

回答by smilezjim

Probably you should try using the date function not 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();