2013-05-13 72 views
0

此脚本的预期用途是将项目添加到类似购物车的物品。当用户点击一个按钮时,应该加载下面的脚本。无法插入表

该脚本首先获取已在表单中输入的产品名称的产品ID。

然后将其放入一个变量中。

插入查询然后执行,使用LAST_INSERT_ID()方法已添加

if(isset($_GET['submit1'])) 
{ 
$db_product_name = $_GET['product_name']; 

$query = "SELECT ProductID FROM product WHERE Product_Name = '$db_product_name'"; 
$result = mysql_query($query) 
or die(mysql_error()); 
$fetch = mysql_fetch_assoc($result); 
$db_productid = $fetch['ProductID']; 

$query = "INSERT INTO `the_shop`.order_line_item(
`OrderID` 
`ProductID` 
) 
VALUES (
`LAST_INSERT_ID()`, `$db_productid`)"; 
$result = mysql_query($query) 
or die(mysql_error()); 
} 

不过,我得到以下错误的最后一个订单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 ' ProductID ) VALUES ( LAST_INSERT_ID() , ..)' at line 3

+0

你忘了(订单id','产品ID) – 2013-05-13 23:49:34

+0

逗号没有冒犯的意图,但如果你遇到麻烦的事情调试像这样的,你_should not_续写销售系统呢。这并不意味着侮辱,我们一直都在你身边,但在处理金钱时犯了错误会产生很大的后果。 – Basic 2013-05-14 00:28:27

回答

2

你有错过了逗号后OrderID

INSERT INTO `the_shop`.order_line_item(
    `OrderID`, -- <---- here is a missed comma 
    `ProductID` 
) 

Mysql总是指向一个部分o f它无法解析的查询。这意味着,右发生之前语法错误引用部分

+0

关于错过的逗号,这些值也包含反引号,当它应该是一个单引号':)' – 2013-05-14 00:02:54

+0

@JW웃:这是一个作业:-)现在OP(我希望)知道如何解释错误消息 – zerkms 2013-05-14 00:06:00

+0

只是将反引号改为单引号,但它再次停止工作。 不要做作业。 这是销售订单处理系统的一部分 – 2013-05-14 00:08:25