Search code examples
phpmysqlpdoprepared-statement

Datetime's NOW() function does not work with PDO-MYSQL prepared statements?


I have a database table timetable with a DATETIME field called created_on.

created_on DATETIME,
notes VARCHAR(255) NOT NULL

I use the NOW() function to insert the current time through PDO prepared statements.

INSERT INTO timetable(created_on, note)
VALUES(?, ?);

So after binding the values and executing the prepared statement...

$notes ="This is a note...";
$values =array("NOW()", $notes);
$stm->execute($values);

... the notes column in the MySQL table writes the expected data but the created_on column produces...

0000-00-00 00:00:00 

I don't want to use the Unix epoch timestamp. So how do I get DATETIME in the NOW() function to work?


Solution

  • don't pass NOW() as a parameter,

    INSERT INTO timetable(created_on, note) VALUES(NOW(), ?);
    

    so you will only need to bind the notes.

    $values =array($notes);