2010-05-26 175 views
0

我得到这个错误:INSERT INTO错误的MySQL/PHP

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 (total, addy, cc) VALUES ('798' , '123 sadf' , '12124123')' at line 1

$total = addslashes(($_SESSION['total'])); 

$addy = addslashes(($_POST['addy'])); 

$cc = addslashes(($_POST['cc'])); 

echo "$total"; 

echo "$addy"; 

echo "$cc"; 

mysql_query("INSERT INTO order (total, addy, cc) VALUES ('$total' , '$addy' , '$cc')") or die(mysql_error()); 

我怎样才能解决这个问题呢?

+0

不要使用任何关键字或备用名称字段或表名。重命名订单。 – Karthik 2010-05-26 05:16:01

回答

7

在SQL中order是保留字。而是使用:

INSERT INTO `order` 
3

这是为什么失败的原因是因为“订单”是SQL中关键字

您需要将反引号放在表名的周围以避免类似问题。

2

尝试把各地为了

`order` 
-3

蜱你忘了“$连接”

mysql_query("INSERT INTO order (total, addy, cc) VALUES ('$total' , '$addy' , '$cc')",$connection) or die(mysql_error()); 
+0

“$连接”是导致这个问题的东西... – Vimard 2010-05-26 05:21:37

+0

对不起朋友,你不知道 – 2010-05-26 10:07:56

+0

Col. Shrapnel是正确的。他得到一个MySQL错误的事实表明连接已经建立。如果没有指定mysql_query()函数,默认情况下会使用已建立的连接。连接不是问题。阅读说明书 - > http://us2.php.net/manual/en/function.mysql-query.php – Joseph 2010-05-26 15:35:32