php获取插入错误

php获取插入错误,php,mysql,insert,Php,Mysql,Insert,我将插入mysql错误,我的代码涉及数组,所以这部分代码没有使用数组,但代码是与数组一起提供的,供其他功能使用 当我尝试在mysql中插入新记录时,插入代码给了我错误 错误说 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 ''product_name') VALUES ('Ar

我将插入mysql错误,我的代码涉及数组,所以这部分代码没有使用数组,但代码是与数组一起提供的,供其他功能使用

当我尝试在mysql中插入新记录时,插入代码给了我错误

错误说

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 ''product_name') VALUES ('Array' where product_id='3246')' at line 1 ....
这是给

$query="INSERT INTO product ('product_name') VALUES ('".$product_name."' where product_id='$product_id[$i]') ";
然后删除“”。。。像这样

$query="INSERT INTO product (product_name) VALUES ('".$product_name."' where product_id='$product_id[$i]') ";
新的错误是

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 'where product_id='3246')' at line 1
然后我删除了“where product_id=。。。。像这样

$query="INSERT INTO product (product_name) VALUES ('".$product_name."' where product_id=$product_id[$i]) ";
现在新的错误

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 'where product_id=3246)' at line 1
我做错了什么?如何解决


AM

就像你说的,你的sql语法是错误的

你应该使用

$query="INSERT INTO product (product_name,product_id) VALUES ('$product_name', '$product_id[$i]') ";
插入新记录


要更新一个已存在的

语句,您的
INSERT
语句中不能有
WHERE
条件。也许您正试图更新现有记录

$query="UPDATE product SET product_name = '".$product_name."'
WHERE product_id=$product_id[$i]";

为什么要将
$product\u name
连接起来,而不是
$product\u id[$i]
插入时不允许使用
条件。为什么在
INSERT
语句中有
where
条件?在
$product\u id[$id]之前还有一个
'
我已经尝试了用户名gbestard中的两个代码,但由于第一个插入代码是“重复输入'3246'用于键'PRIMARY'”,因此这两个代码都不起作用。第二个插入代码给出了相同的旧错误。因此,没有任何其他想法!也。。对于用户名令牌,我的案例涉及复制记录并作为新记录插入。因此,这必须使用插入而不是更新。如果您只想修改现有记录,则第一个查询将失败,但第二个查询应该可以工作,您会遇到什么错误?因此我更改了插入代码,如下所示$query=“在产品(产品名称、产品类别)中插入值(“$product\U name.”、“$product\U category.”);但是错误表明列计数与第1行的值计数不匹配。。。。。此错误是否表示列名不匹配?是否正确!name和category是字符串,所以需要添加一些单引号来环绕它们。您使用变量来保存名称,但在构造SQL查询时,它们还需要用引号括起来的文本$query=“在产品(产品名称)中插入值(“.$product\U name[i]”);是我放“$product_name[i]”的地方。是否正确!是的,我想你明白了:)你需要更具体,而且你已经接受了答案。现在有什么问题?
$query="UPDATE product SET product_name = '".$product_name."'
WHERE product_id=$product_id[$i]";