-4

我目前收到错误消息

Could not enter data: 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(quantity, product)VALUES ( "number", "text")' at line 1

代码如下:

<?php
require_once('../../config/db.php');
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if(! $conn )
{
  die('Error with database connection: ' . mysql_error());
}
$sql = 'INSERT INTO order'.
       '(quantity, product)' .
       'VALUES ( "number", "text")';

mysql_select_db('order');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>

有人可以让我知道我做错了什么以及如何纠正错误吗?我正在使用 MySQL 5.5.27 和 PHP 5.5。

4

2 回答 2

4
'INSERT INTO  `order` '.
       '(quantity, product)' .
       'VALUES ( "number", "text")';

使用 blockquotes `作为转义字符来使用像order 这样的保留字

于 2013-07-07T08:50:20.590 回答
-2

在 sql 中,字符串值使用单引号,而不是双引号。所以让我们试试这样:

    $sql = 'INSERT INTO order'.
   '(quantity, product)'.
   'VALUES ( \'number\', \'text\')';

或者像这样

    $sql = "INSERT INTO order".
   "(quantity, product)".
   "VALUES ( 'number', 'text')";

其次,如果“数量”的类型是数字,则在此处传递一个数值:

    $sql = "INSERT INTO order".
   "(quantity, product)".
   "VALUES ( 100500, 'text')";
于 2013-07-07T08:54:56.087 回答