Forum Moderators: coopster

Message Too Old, No Replies

MySQL PHP Form Insert Into as array problem

MySQL PHP Form Insert Into as array problem

         

knippysing

3:24 pm on Jun 2, 2006 (gmt 0)

10+ Year Member



I have a problem with a current form. I have a simple form with some check boxes that are pulled from a database and populated on the page. From there, I want to insert each item that's selected into a database as their own line.
So far I've been able to get that accomplished for the most part but only with a "foreach" statement on one item.
Here is the code on the form page:

<?PHP
require './connect.php';
print "<form action='pm_append_data.php' method='post'>";
$var = $_GET['var'];
$var2 = $_GET['var2'];
$var3 = $_GET['var3'];
$sqlquery = "SELECT Equip.ID, AgrTask.CustNo, AgrTask.LocNo, AgrTask.AgrmtNo, AgrTask.MasterTask, AgrTask.Task, AgrTask.Level, AgrTask.Skill, AgrTask.Time, AgrTask.LongDesc, AgrTask.SubTotal, AgrTask.Counter
FROM (AgrTask INNER JOIN Task ON AgrTask.Task = Task.Task) INNER JOIN Equip ON AgrTask.AgrmtNo = Equip.SerAgrNo
WHERE (((Equip.ID)='$var') AND ((AgrTask.CustNo)='$var3') AND ((AgrTask.MasterTask) Like '%$var2') AND ((AgrTask.LongDesc) Not Like ''))";
$result = mysql_query($sqlquery) or die (mysql_error());
$num=mysql_num_rows($result);
$i=0;

while ($i < $num) {
$a=mysql_result($result,$i,"LongDesc");
$b=mysql_result($result,$i,"CustNo");
$c=mysql_result($result,$i,"AgrmtNo");
$d=mysql_result($result,$i,"Task");
$e=mysql_result($result,$i,"Counter");
$f=mysql_result($result,$i,"MasterTask");
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'>&nbsp;-&nbsp;$a</font></td>";
print "<input name='Schedule' type='hidden' Value='$var2'>";
print "<input name='EquipID' type='hidden' Value='$var'>";
print "<input name='CustNo' type='hidden' Value='$b'>";
print "<input name='AgrmtNo' type='hidden' Value='$c'>";
print "<input name='Task' type='hidden' Value='$d'>";
print "<input name='Counter' type='hidden' Value='$e'>";
print "<input name='MasterTask' type='hidden' Value='$f'>";

++$i;
}
?>

and the handling page:


<?
$EquipID=$_POST['EquipID'];
$AgrmtNo=$_POST['AgrmtNo'];
$Counter=$_POST['Counter'];
$MasterTask=$_POST['MasterTask'];
$Task=$_POST['Task'];
$CustNo=$_POST['CustNo'];
$Completed_By=$_POST['Completed_By'];
$Itemss=$_POST['Items_Completed'];
$Schedule=$_POST['Schedule'];

$db="database";
$link = mysql_connect("localhost", "database", "password");
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
foreach ($Itemss as $value){
mysql_query("INSERT INTO PM_History (EquipID, AgrmtNo, Counter, MasterTask, Task, CustNo, Completed_By, Items_Completed, Schedule) Values ('$EquipID', '$AgrmtNo', '$Counter', '$MasterTask', '$Task', '$CustNo', '$Completed_By', '$value', '$Schedule')");
}
echo "PM Information Successfully Added to History!";
mysql_close($link);
?>

I need each line that's inserted into the database to have its own unique data rather than having the same information in all the fields except for the "Items_Completed" field.
Make sense? Ideas?

coopster

8:53 pm on Jun 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think I understand, but am not quite certain. Do you want to associate each option with it's selected checkbox option "group" so to speak? If so, you could use a counter. I notice you already have a counter initiated and being used to offset row colors, so let's incorporate that as part of the indexing then. Here is what I mean, if I understand you correctly:
$i=0; 
while ($i < $num) {
...
print "<td align=left><font Times size=3 px><input name='Items_Completed[group$i]'
type='checkbox' value='$a'>&nbsp;-&nbsp;$a</font></td>";
print "<input name='Items_Completed[group$i][Schedule]' type='hidden' Value='$var2'>";
print "<input name='Items_Completed[group$i][EquipID]' type='hidden' Value='$var'>";
...
++$i;
}

Now in your processing script you can pull them out in groups.