简体   繁体   English

在SQL语法和PHP中出错

[英]Getting error in SQL syntax and PHP

I am getting the following error and can't figure out what the problem is. 我收到以下错误,无法找出问题所在。

Error: You have an error in your SQL syntax; 错误:您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order (orderid, customerid, productid, brand, model, price, amount, totalcost) V' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的“订单(订单ID,客户ID,产品ID,品牌,型号,价格,数量,总成本)V”附近使用

//connect to database
$connection = mysql_connect("localhost","root","") or die ("Can't connect");
mysql_select_db("shoppingcart", $connection) or die ("Can't connect");

//get order id
$vol = mysql_query("SELECT orderid FROM ordertracking WHERE email='$email'");
while($volume=mysql_fetch_array($vol)) {
  $orderid = $volume['orderid'];
}
echo $orderid;
// add new order
$order = "INSERT INTO order (orderid, customerid, productid, brand, model, price, amount, totalcost) VALUES ('$orderid', '$customerid', '$productid', '$brand' , '$model', '$price', '$amount', '$totalcost')";
if (!mysql_query($order,$connection)) {
  die('Error: ' . mysql_error());
  echo "Sorry, there was an error";
}
echo "New order added" . "<br />";
mysql_close($connection);

ORDER is a mysql resered word enclose it in backticks ``. ORDERmysql重新定义的单词,将其括在反引号中。

You should not have a table or a column name conflicting with mysql reserved words otherwise you must have to enclose those in backticks. 您的表或列名称不应与mysql保留字冲突,否则必须将其括在反引号中。

$order = "INSERT INTO `order` (orderid, customerid,...

Like this: 像这样:

INSERT INTO `order` (...) (ALT Gr+7) 插入`order`(...)(ALT Gr + 7)

If this solves the problem, give the credits to Shakti Singh. 如果这样可以解决问题,请感谢Shakti Singh。

The keyword ORDER is a reserved keyword in sql. 关键字ORDER是sql中的保留关键字。 Means you cannot use it 表示您无法使用它

so this will produce an error: 所以这会产生一个错误:

$order = "INSERT INTO order (orderid, customerid, productid, brand, model, price, amount, totalcost) VALUES ('$orderid', '$customerid', '$productid', '$brand' , '$model', '$price', '$amount', '$totalcost')";

Insert into ORDER 插入订单

the order in the above statement should either be in backticks (as Mentioned by shakti) like 上面声明中的顺序应该是反引号(如shakti所述)

INSERT INTO 'order' (orderid,...... 插入'order'(orderid,......

or you can enclose the reserved keywords in square brackets like 或者您可以将保留的关键字放在方括号中,例如

INSERT INTO [order] (orderid,...... 插入[order](orderid,......

for more information check this thread stack overflow question 有关更多信息,请检查此线程堆栈溢出问题

something else is that any code after die will not work i mean : 另一件事是,死后的任何代码将无法正常工作:

if (!mysql_query($order,$connection)) {
 die('Error: ' . mysql_error());
 echo "Sorry, there was an error";}

this code 此代码

echo "Sorry, there was an error";

will not work.and using this code : 将不起作用。并使用此代码:

die('Error: ' . mysql_error());

is not recomended at all due to security reasons 由于安全原因,完全不推荐使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM