Forum Moderators: coopster

Message Too Old, No Replies

Form data dynamic with checkbox- need to insert ALL selections into DB

Form data dynamic with checkbox- need to insert ALL selections into DB

         

knippysing

9:19 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



I have a form that I am generating line items with checkboxes dynamically from a Database. When someone checks more than one box, only the last one is recorded to my database history. I need to record ALL selections made by the check boxes.

Here's the code I'm currently using (some additional form data is after it but I figured I would just post the relavant part):
-----------------------------
<?PHP
print "<form action='pm_append_data.php' method='post'>";
$var = $_GET['var'];
$sqlquery1 = "SELECT DISTINCT Task.Desc
FROM Task INNER JOIN (AgrTask INNER JOIN (AgrSched INNER JOIN Equip ON AgrSched.CustNo = Equip.CustNo) ON (AgrTask.AgrmtNo = Equip.SerAgrNo) AND (AgrTask.CustNo = Equip.CustNo)) ON Task.Task = AgrTask.Task
WHERE Equip.ID='$var'";
$result = mysql_query($sqlquery1) or die (mysql_error());

$num=mysql_num_rows($result);
$i=0;

while ($i < $num) {
$a=mysql_result($result,$i,"Desc");

print ($i % 2)? "<tr bgcolor=\"F0F0F0\">" : "<tr bgcolor=\"FFFFFF\">";

print "<td align=left><font Times size=3 px><input name='Items_Completed' type='checkbox' value='$a'>$a</font></td>";

++$i;
}
?>
----------------------------
The "Items Completed" is what I need a running total of or an array submitted into my DB.

The handling form is as follows:
----------------------------
<?
$user=$_POST['#*$!#*$!xxxxx'];
$password=$_POST['xxxxxxxxxxx'];
$EquipID=$_POST['var'];
$PM_Sched=$_POST['PM_Sched'];
$Completed_By=$_POST['Completed_By'];
$Items_Completed=$_POST['Items_Completed'];
$Last_PM_Complete=$_POST['Last_PM_Complete'];

$db="xxxxxxxxx";
$link = mysql_connect("localhost", "xxxxxxxxx", "xxxxxxxxxx");
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
mysql_query("INSERT INTO PM_History (EquipID, PM_Sched, Completed_By, Items_Completed, Last_PM_Complete) Values ('$EquipID', '$PM_Sched', '$Completed_By', '$Items_Completed', '$Last_PM_Complete')");
echo "PM Information Added to History!";
mysql_close($link);
?>
--------------------

Any help would be great appreciated!
Thanks again.

onematchfire

9:36 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



You'll need to change the form field name to "Items_Completed[]" so the PHP knows to expect an array of values. Right now each line's value gets overwritten by the next one.

It's an easy gotcha I recall stumbling over myself.

knippysing

9:56 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



Thanks much! that did the trick. Love PHP, just a lot of turns in it!

Anyone recommend reading for intermediate learning?

Thanks again!

coopster

1:04 pm on Jun 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The manual covers many of these little stumbling blocks.

PHP and HTML [php.net]

knippysing

2:01 pm on Jun 29, 2005 (gmt 0)

10+ Year Member



Well, I thought it worked because I saw data entered into the database but instead of the individual line items that I clicked on getting placed in the database field, I get the word "Array" entered in.

Here's the line that I changed per above:
print "<td align=left><font Times size=3 px><input name='Items_Completed[]' type='checkbox' value='$a'>$a</font></td>";

thanks

dreamcatcher

2:41 pm on Jun 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your problem is you are echoing the $Items_Completed variable, which is an array. You need numerical indices to access an array unless you use a foreach loop.

In your case if you are trying to insert the collective value of checkboxes into one field, use implode.

$items = implode(",",$Items_Completed);

Then use $items as your variable.

dc

knippysing

3:24 pm on Jun 29, 2005 (gmt 0)

10+ Year Member



That did the trick. Once again you guys are brilliant!