PHP - Inserting Multiple Dynamic Checkbox Values

liunx

Guest
Hi everyone,

I need some assistance inserting multiple dynamic checkbox values into a table in my database.

Currently, I am able to generate the dynamic checkboxes in a form and have one of the checked values insert into my table. However, I need all values checked by the user to be inserted.

Posted below is the code I am utilizing. Thanks in advance for any assistance :)



Form:

echo "<table border=\"1\" cellpadding=\"5\" cellspacing=\"5\"><tr><td>";
echo "<FORM ACTION=\"$Relative/addCart.php\" METHOD=\"POST\">";
blueFont("Arial","<br><br><INPUT TYPE=\"checkbox\" NAME=\"ItemID\" VALUE=\"$II\" CHECKED> Add ");
blueFont("Arial","<INPUT TYPE=\"TEXT\" Name=\"ItemQuantity\" Value=\"1\" size=\"2\"> of");
blueFont("Arial"," this item to my shopping cart<br><br>");
echo "<INPUT TYPE=\"hidden\" NAME=\"UID\" VALUE=\"$UID\">";


$result = mysql_query($query);
while(list($AI, $AT) = mysql_fetch_row($result))
{echo "<INPUT TYPE=\"checkbox\" NAME=\"Attributes\" VALUE=\"$AT\">
$AT";}

$Date=date("Y:z:Y-m-d");
echo "<input type=\"hidden\" name=\"Date\" value=\"$Date\">";
echo "<input type=\"hidden\" name=\"CartItemsID\" value=\"\">";
echo "</td><td><br><br><INPUT TYPE=\"SUBMIT\" VALUE=\"Add This Item To My Cart\"></td></tr></table></FORM>";

commonFooter($Relative,$UID);



addCart:

<?php
require("Cart.php");
DBinfo();
Root();
mysql_connect("$DBHost","$DBUser","$DBPass");
mysql("$DBName","INSERT INTO CartItems VALUES
('$UID','$ItemID','$ItemQuantity','$Date','$CartItemsID','$Attributes')");
Header("Location: $Relative/viewCart.php?UID=$UID");
?>uhh you can't do that.


$result = mysql_query($query);
while(list($AI, $AT) = mysql_fetch_row($result))
{echo "<INPUT TYPE=\"checkbox\" NAME=\"Attributes\" VALUE=\"$AT\">
$AT";}

all those checkboxes will have the same name. so it will only grab the last one it sees on submit. you have to make it an array and then make a loop when you do the insert


$result = mysql_query($query);
while(list($AI, $AT) = mysql_fetch_row($result))
{echo "<INPUT TYPE=\"checkbox\" NAME=\"Attributes[]\" VALUE=\"$AT\">
$AT";}

<?php
require("Cart.php");
DBinfo();
Root();
mysql_connect("$DBHost","$DBUser","$DBPass");
foreach($_POST["Attributes"] AS $value){
mysql("$DBName","INSERT INTO CartItems VALUES
('$UID','$ItemID','$ItemQuantity','$Date','$CartItemsID','$Attributes')");
}
Header("Location: $Relative/viewCart.php?UID=$UID");
?>uhh you can't do that... I guess that would explain why I'm having so much trouble - ;) (I'm kind of new to the world of PHP)


Thank you for your help Scoutt. Unfortunately, it didn't quite do the trick. I tried the PHP code supplied above, but here's what happened:

- The word "array" is inserted into the database rather than the actual values.

- The number of items added to the cart is doubled for each checkbox that was checked. (I.e. If I attempted to add 1 item to the cart and checked 3 boxes, the 1 item is added 3 times).

Do you have any other suggestions or ideas?

Thanks again :Dwell I was just going form what you told me, I have no idea how your db is setup so I am not sure. so, my mistake was in the foreach loop,

foreach($_POST["Attributes"] AS $value){

should be

foreach($_POST["Attributes"] AS $key => $value){

also did you want each checkbox inserted in the same row? then jsut take that loop out and use something like this

mysql("$DBName","INSERT INTO CartItems VALUES
('$UID','$ItemID','$ItemQuantity','$Date','$CartItemsID','{$_POST["Attributes"][0]} {$_POST["Attributes"][1]} {$_POST["Attributes"][2]} {$_POST["Attributes"][3]}')");
}

depending on how many check boxes you have. no thte amount you have shecked but the amount you have showing in the form field.:D

Thanks Scoutt

You got it on that one! I was actually attempting to have each value insert into the same field.

My apologies, for not giving you enough information. It must be terribly difficult to try to make sense out of some of these posts at times.

Thanks again for all of your help!You could probably make a small change to what you had before and do it this way:$result = mysql_query($query);
while(list($AI, $AT) = mysql_fetch_row($result))
{echo "<INPUT TYPE=\"checkbox\" NAME=\"Attributes[]\" VALUE=\"$AT\">
$AT";}When you post the form $_POST['Attributes'] will be an array which you could loop round. Anyway, seems you got it working now.isn't that the way I had it Torrent ;)<note to self>Must..read..posts..before..answering :D :D :rolleyes:LOL

<note to self>Must..slow..down..on..the..beer :D:D or share :P
 
Back
Top