SQL语法出错,很可能是我的值

时间:2013-07-07 08:33:23

标签: php mysql sql

我目前收到错误消息

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。

2 个答案:

答案 0 :(得分:4)

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

使用blockquotes `作为转义字符,以使用订单

等备用字词

答案 1 :(得分:-2)

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

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

或者像这样

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

其次,如果'quantity'的类型是数字,则在那里传递一个数值:

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