此脚本的预期目的是将项目添加到类似于购物车的东西中。当用户单击按钮时,应加载下面的脚本。
脚本首先获取已在表单中输入的产品名称的产品 ID。
然后将其放入变量中。
然后使用 LAST_INSERT_ID() 方法执行 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());
}
但是我收到以下错误:
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
最佳答案
您在 OrderID
后面漏掉了逗号
INSERT INTO `the_shop`.order_line_item(
`OrderID`, -- <---- here is a missed comma
`ProductID`
)
Mysql 总是指向它无法解析的查询的一部分。这意味着语法错误发生在引用部分之前
关于php - 无法插入表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16532933/