johnny
johnny

Reputation: 15

mysql INSERT for loop trouble

I've been using this for loop to insert information into my database:

$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).")";

However, when I looked at the result on the webpage, it gave me this message:

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 

Can someone help solve this issue?

Upvotes: 1

Views: 69

Answers (1)

Peter Pei Guo
Peter Pei Guo

Reputation: 7870

Very likely the problem is the missing quotes, and you probably wanted something like the following for your values portion:

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

Which gives you something like:

'abc','xyx','123'

Of course I am assuming that they are all of string type. If some are not, then you need to make sure strings are quoted and numbers are not etc.

The best is for sure to use place holders, then you do not need to go through this trouble at all.

Upvotes: 1

Related Questions