简体   繁体   English

使用PHP将时间戳插入MySQL

[英]Inserting timestamp to MySQL using PHP

I am trying to insert timestamp to my MySQL database using PHP. 我正在尝试使用PHP将时间戳插入我的MySQL数据库。 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 警告:PDOStatement :: execute():SQLSTATE [HY093]:无效的参数编号:参数未在第22行的/opt/lampp/htdocs/e-exam/user.message.php中定义

But

Here line 22 is ':sent_at' => $timestamp 第22行是':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. 如果只想插入send_at(当前时间),则有一种更简单的方法,首先使用NOW()创建表。

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. 查询中的NOW()占了很多。

you can try the query 您可以尝试查询

select NOW()

it will result something like this 2018-07-04 18:17:49 它会导致像这样的事情2018-07-04 18:17:49

in mysql you can use data type datetime 在MySQL中,您可以使用数据类型datetime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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