简体   繁体   中英

Inserting timestamp to MySQL using PHP

I am trying to insert timestamp to my MySQL database using PHP. But there is shown an error off

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in /opt/lampp/htdocs/e-exam/user.message.php on line 22

But

Here line 22 is ':sent_at' => $timestamp

 $timestamp = date('Y-m-d G:i:s');

      //message sending
      $sql3 = "INSERT INTO message(title, content, sender_id, sent_at ) VALUES (:title, :content, :sender_id, :sent_at)";
      $stmt3 = $pdo->prepare($sql3);
      $stmt3->execute(array(
        ':title' => $_POST['title'],
        ':content' => $_POST['content'],
        ':email' => $_SESSION['active_user'],
        ':sent_at' => $timestamp
      ));

Please help me to solve this error

If you only want to insert the sent_at ( current time ) , there is easier way, by using NOW() at first you create the table.

the other way is by change the line into this

"INSERT INTO message(title, content, sender_id, sent_at ) VALUES (:title, :content, :sender_id, NOW())";

take a lot at NOW() in the query.

you can try the query

select NOW()

it will result something like this 2018-07-04 18:17:49

in mysql you can use data type datetime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM