php - MySQL PHP : Geting 0000-00-00 00:00:00 when using NOW() in a datetime column

标签 php mysql

在下面的代码中使用SQL:

public function saveOrder() {
  $id = $this->getUserId();
  $this->db->query("INSERT INTO orders VALUES (null, '$id', '$this->basket_lines', '$this->total', NOW() )");
  return $this->db->id();  
}

上面最后一列是 DATETIME字段,数据库中的结果保持默认为 0000-00-00 00:00:00 .

我试过列格式为 timestamp也用过:

ALTER TABLE  `content ` CHANGE  `date`  `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

来自另一个帖子,但无济于事。

谁能找出问题所在?

谢谢

编辑:顺便说一句,我正在使用

从 DB 类中转义我的 SQL 语句
    $this->SQL = $this->mysqli->real_escape_string($SQL);
    $this->result = $this->mysqli->query($SQL);

编辑 2:我现在使用 来逃避预查询

 $i=mysql_real_escape_string($id);
    $b=mysql_real_escape_string($this->basket_lines);
    $t=mysql_real_escape_string($this->total);
    $this->db->query("INSERT INTO orders VALUES (null, '$i', '$b', '$t', NOW() )"); 

还是不行,这一切都很奇怪?

最佳答案

您没有转义任何数据。您的数据很可能会打乱您的其余查询。您也很可能对 SQL 注入(inject)持开放态度。至少,您应该使用 mysql_real_escape_string() , 但最好使用 prepared queries with PDO .

关于php - MySQL PHP : Geting 0000-00-00 00:00:00 when using NOW() in a datetime column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10029999/

相关文章:

php - 在 CentOS 6 上将 PHP 从 7.1 升级到 7.3/7.4

mysql - 性能改进 : Left outer Join, Order By, Subquery: Index not picked by the main query

php - Inner Join 多次上市

mysql - MySQL 表名的数学运算

php - 使用两个循环遍历 mysql 表

php - 如何防止PHP中的SQL注入(inject)?

php - 是否可以使用 javascript 查找文件的最后修改时间?

php - 魔术方法 php,__call __get 和 __set?

mysql - 1044 - 拒绝用户 'user' @'localhost' 访问数据库 'db'

java - Spring Boot - “Error creating bean with name 'entityManagerFactory'” - 启动