【问题标题】:Inserting integer and datetime from PHP to MySQL 5.0 db error将整数和日期时间从 PHP 插入 MySQL 5.0 db 错误
【发布时间】:2011-03-27 20:21:21
【问题描述】:

我试图找出问题所在。

表是conditions_loop。一列是condition_id,另一列是日期时间类型。

代码是这样的

$dt = date("Y-m-d H:i:s");

mysql_query("INSERT INTO conditions_loop (condition_id, date) VALUES ($latest_condition, $dt)") or die(mysql_error());  

$latest_condition 是一个 1 位整数。

错误说 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“13:12:14)”附近使用正确的语法

我什么都试过了,但我不知道。感谢阅读。

【问题讨论】:

    标签: php datetime mysql-5.0


    【解决方案1】:

    你应该引用日期值:

    mysql_query("INSERT INTO `conditions_loop` (`condition_id`, `date`) 
             VALUES ('$latest_condition', '$dt')") or die(mysql_error());  
    

    当您使用它时,也可以使用反引号 (`) 引用表/字段名称

    【讨论】:

    • ...但引用其他值不会有什么坏处。
    • 感谢您提醒我这个符号`我从未在键盘上使用过它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 2011-02-25
    • 2013-07-26
    • 1970-01-01
    • 2016-08-20
    相关资源
    最近更新 更多