Hi All
I really should know how to do this by now but just can’t get it to work.
I have a series of tick boxes called itemid all with a different number.
I have my table in the database with a few fields e.g. uniqueid, catid, itemid.
I need to add each of the itemid’s that has been ticked into the database.
I have tried a foreach statement with no luck.
Any example code would be great and if you need to know more let me know.
Thanks in advance.
mrmbarnes
That was easier…
The code used was:
foreach($_POST[“itemid”] as $itemid) { $result=mysql_query(“INSERT INTO shoppingcartmultiitemdetails (siteid, catid, itemid) VALUES (‘$accountname’, ‘$uniqueid’, ‘$itemid’)”); }
Where I went wrong was I forgot to put the in the tick box e.g. itemid
A hint: do you think it’s more efficient to issue multiple “INSERT INTO … VALUES” mysql queries or just one?
And a tip as well
Sanitize all user input before using it in a query.
mrmbarnes:
That was easier…
The code used was:
foreach($_POST[“itemid”] as $itemid) { $result=mysql_query(“INSERT INTO shoppingcartmultiitemdetails (siteid, catid, itemid) VALUES (‘$accountname’, ‘$uniqueid’, ‘$itemid’)”); }
Where I went wrong was I forgot to put the in the tick box e.g. itemid
nothing wrong with the insert statement.
Note: The itmeid must be array type on the form.
I’m a bit… well, I am not confused but I simply cannot bring myself to understanding why would anyone explain what was wrong with the query since the OP didn’t say he has a problem with it and he managed to resolve that he needs to specify an array so - why would you advise him about what he managed to do already?
I think that the OP meant that he forget to use the brackets on the checkbox variable names in the HTML. I’ve done that mistake too.
It seems that communication gap is huge in this topic so I’ll just point out that what Guido said about cleaning the input is important, so pay attention to that. It’ll make you sleep better knowing no one can hack your database
If I were you I’ll use this
$fields = $_POST[‘var1’].‘|X|’.$_POST[‘var2’].'|X|.$_POST[‘var3’] ;
for inserting to table
when I select data I’ll explode it with explode() ;
eg.,
$explo = explode(‘|X|’,$fields) ;
$f1 = $explo[0] ; $f2 = $explo[1] ; $f3 = $explo[2] ;
somethings like this.
And if I were me, I’d strongly advise against this terrible “advice” that makes absolutely no sense at all.