mysql INSERT for循环麻烦

时间:2014-10-08 01:38:32

标签: php mysql loops

我一直用这个for循环将信息插入我的数据库:

$values = array();
for($x=1;$x<=3;$x++){
    $values[]= $_POST["FCKeditor".$x]; 
}
echo implode(",",$values);

$sql = "INSERT INTO virus (v1,v2,v3) VALUES(".implode(",",$values).")";

然而,当我在网页上查看结果时,它给了我这样的信息:

a1
,b2
,c3
INSERT INTO virus (v1,v2,v3) VALUES(a1
,b2
,c3
)You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '>,b2
,c3
)' at line 1 

有人可以帮助解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

很可能问题是缺少引号,您可能想要类似以下内容的值:

"'".implode("','",$values)."'"

这给你一些类似的东西:

'abc','xyx','123'

当然我假设他们都是字符串类型。如果有些不是,那么你需要确保引用字符串并且数字不是等等。

最好的确是使用占位符,然后你根本不需要经历这个麻烦。