Question

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.

Was it helpful?

Solution

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

OTHER TIPS

Probably you should try using the date function not 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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top