【问题标题】:Submit timestamp to mysql database with php mysqli使用 php mysqli 向 mysql 数据库提交时间戳
【发布时间】:2015-02-04 15:07:53
【问题描述】:

我有这个数据库(mysql):

我有代码:

$day = $app->request->post('day');
            $startLocation = $app->request->post('startLocation');
            $datum = new DateTime();
            $startTime = $datum->getTimestamp(); //this line is IMPORTANT
            global $user_id;
            $db = new DbHandler();

            // creating new task
            $day_id = $db->createDay($user_id, $day, $startLocation, $startTime); ETC...

但我无法将timestamp 的数据提交到startTime 的数据库中,女巫有type timestamp

为什么?该怎么做?

【问题讨论】:

  • 你有什么错误吗?如果是,您能否提供错误文本?
  • 不,因为后端是 REST api 服务,我尝试在其上添加数据...我将其发送到数据库:startTime: 1417878370,但该数据未在数据库中提交

标签: php mysql date mysqli timestamp


【解决方案1】:

如果你的列数据类型是TIMESTAMP,那么应该是:

$datum = new DateTime();
$startTime = $datum->format('Y-m-d H:i:s');

注意:->getTimestamp() 返回 unix 时间戳

并修改你的包装函数:

public function createDay($user_id, $day, $startLocation, $startTime) { 
    $stmt = $this->conn->prepare("INSERT INTO days(day,startLocation,startTime) VALUES(?,?,?)"); 
    $stmt->bind_param('sss', $day, $startLocation, $startTime); // change the d into s in your types
    $result = $stmt->execute(); 
    $stmt->close();
}

【讨论】:

  • hm,再次不起作用,我发送此数据:startTime: "2014-12-06 16:10:05" ,但该数据不在数据库中
  • @LaraBeginer 确保提供所有字段,您总共有 8 个字段
  • 可能是我的函数问题: public function createDay($user_id, $day, $startLocation, $startTime) { $stmt = $this->conn->prepare("INSERT INTO days(day,开始位置,开始时间)值(?,?,?)“); $stmt->bind_param("ssd", $day, $startLocation, $startTime); $result = $stmt->execute(); $stmt->close();
  • 谢谢,没错,但使用 bind_param "sss" 可以正常工作......再次感谢
  • @LaraBeginer 是的,那是我要指出的,您现在输入的是字符串,而不是数字。我很高兴这有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 2012-11-03
  • 2011-06-06
  • 1970-01-01
  • 1970-01-01
  • 2017-02-05
  • 1970-01-01
相关资源
最近更新 更多