dongzhenshen7435
2018-06-14 11:39
浏览 391
已采纳

如何手动将时间戳插入mySQL数据库?

I want to manually be able to insert a timestamp value to mySQL database:

  $timestamp = "2018-05-15 00:00:00";
  $name = "Fred";


  $pdo = $db->prepare('INSERT INTO data (timestamp,name) values(:timestamp,:name)');
  $pdo->execute(array(
       ':timestamp' => $timestamp,
       ':name' => $name
  ));

But it inserts always the date of now, not the date of my variable.

MYSQL table:

id | timestamp | name
----------------------
   |           |


id = int(11) AUTO_INCREMENT
timestamp = timestamp (Standard: CURRENT TIMESTAMP, Attribute: ON UPDATE CURRENT TIMESTAMP)
name = varchar (255)

图片转代码服务由CSDN问答提供 功能建议

我想手动将时间戳值插入mySQL数据库: < pre> $ timestamp =“2018-05-15 00:00:00”; $ name =“Fred”; $ pdo = $ db-&gt; prepare('INSERT INTO data (timestamp,name)values(:timestamp,:name)'); $ pdo-&gt; execute(array( ':timestamp'=&gt; $ timestamp, ':name'=&gt; $ name ));

但它总是插入现在的日期,而不是我变量的日期。

MYSQL table:

  id | 时间戳|  name 
 ---------------------- 
 |  | 
 
 
id = int(11)AUTO_INCREMENT 
timestamp = timestamp(标准:CURRENT TIMESTAMP,属性:ON UPDATE CURRENT TIMESTAMP)
name = varchar(255)
   
 <  / DIV>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtong848825 2018-06-14 11:49
    已采纳

    If you want to manually insert date in timestamp field then you should change its type from timestamp (Standard CURRENT TIMESTAMP) to datetime. Only then it will work, otherwise always it save current system time.

    Read more about datetime

    点赞 打赏 评论

相关推荐 更多相似问题