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

JohnUS picture JohnUS · Feb 29, 2012 · Viewed 35.3k times · Source

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

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 = '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_date column like this: 0000-00-00 00:00:00

How can I do this ? Thanks in advance.

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

Answer

Hammerite picture Hammerite · Feb 29, 2012

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

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