【问题标题】:MySQL INSERT statement brings an error [closed]MySQL INSERT 语句带来错误 [关闭]
【发布时间】:2023-03-12 07:50:02
【问题描述】:

我有一个名为 ORDER 的表,标题为 order_date,我需要在其中放置日期。

function NewOrderID()
    {    
        $date = date('Y-m-d H:i:s');
        $t = "INSERT INTO order (order_date) 
              VALUES (' ".$date." ')";
          $query = sprintf($t);    
          $result = mysql_query($query);                        
          if (!$result)
          die(mysql_error());
          return  mysql_insert_id();
    }

我收到以下错误:

    You have an error in your SQL syntax; check the manual that corresponds to your 
    MySQL server version for the right syntax to use near 'order ('order_date')
 VALUES (' 2014-04-29 15:02:40 ')' at line 1

有什么想法让我搞砸了查询?

【问题讨论】:

标签: php mysql sql


【解决方案1】:

order是MySQL中的保留字,可以在表名前后使用反引号(`)

你也可以使用 MySQL NOW() 函数来代替 PHP 变量 $date

$t = "INSERT INTO `order` (order_date) VALUES (NOW())";

【讨论】:

    【解决方案2】:

    首先,您正在使用 mysql_* 函数,这些函数现在已被弃用,将来会从 PHP 中删除。所以你需要开始改用 MySQLi 或 PDO。

    您收到 MYSQL 错误是因为:

    orderMYSQL 的保留关键字,因此您必须在表名周围使用反引号`:

    $t = "INSERT INTO `order` (order_date) VALUES ('".$date."')";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      相关资源
      最近更新 更多