Updating multiple row with multiple values
Posted: Mon Dec 18, 2006 11:07 am
Good morning! I'm a php noob and am hoping somebody can help me out...
I'm creating a php/MySQL application to manage in-house contact lists. Each contact con be associated to many communities, and each community can have upto 15 items that can be set (checkboxes/bool)
A snippet of the display/edit form is:
And the Save php file snippet is:
So for instance if this person was associated to 4 communities but HOContact was only set for 2 of them (say 2nd and last) then the $query2 will put the 'on' for records 1 & 2 instead of where it should be.
I think the easiest thing might be to set the off flag for the check boxes not selected, but I'm having trouble figuring that out.
Or maybe there is a completely different way to do it...
Thanks for you time.
Wade
I'm creating a php/MySQL application to manage in-house contact lists. Each contact con be associated to many communities, and each community can have upto 15 items that can be set (checkboxes/bool)
A snippet of the display/edit form is:
Code: Select all
<?php
mysql_data_seek( $result1, 0 );
while ($row1 = mysql_fetch_assoc($result1)) { ?>
<input type="hidden" name="rowData[]" value="<?php echo $row1 ?>" />
<tr><td><input type="text" value="<?php echo $row1['CName']; ?>" name="CName[]" size="15" /></td>
<td><input type="checkbox" name="HOContact[]" <?php if($row1["HOContact"]){echo " CHECKED";} ?> /></td>
<td><input type="checkbox" name="HOAcctContact[]" <?php if($row1["HOAcctContact"]){echo " CHECKED";} ?> /></td>
<td><input type="checkbox" name="MUAreaMgr[]" <?php if($row1["MUAreaMgr"]){echo " CHECKED";} ?> /></td>
<td><input type="checkbox" name="StAreaMgr[]" <?php if($row1["StAreaMgr"]){echo " CHECKED";} ?> /></td>
<td><input type="checkbox" name="MFAreaMgr[]" <?php if($row1["MFAreaMgr"]){echo " CHECKED";} ?> /></td>
<td><input type="checkbox" name="SCContact[]" <?php if($row1["SCContact"]){echo " CHECKED";} ?> /></td>
<td><input type="checkbox" name="VMAttendee[]" <?php if($row1["VMAttendee"]){echo " CHECKED";} ?> /></td>
<td><input type="checkbox" name="ConMgr[]" <?php if($row1["ConMgr"]){echo " CHECKED";} ?> /></td>
<td><input type="checkbox" name="SiteSuper[]" <?php if($row1["SiteSuper"]){echo " CHECKED";} ?> /></td>
<td><input type="checkbox" name="SiteSuperAsst[]" <?php if($row1["SiteSuperAsst"]){echo " CHECKED";} ?> /></td>
<td><input type="checkbox" name="VIPOpenInvite[]" <?php if($row1["VIPOpenInvite"]){echo " CHECKED";} ?> /></td>
<td><input type="checkbox" name="CCCLunchInvite[]" <?php if($row1["CCCLunchInvite"]){echo " CHECKED";} ?> /></td></tr>
<?php } ?>Code: Select all
$rowData = $_POST["rowData"];
$CName = $_POST["CName"];
$HOContact = $_POST["HOContact"];
$HOAcctContact = $_POST["HOAcctContact"];
$MUAreaMgr = $_POST["MUAreaMgr"];
$StAreaMgr = $_POST["StAreaMgr"];
$MFAreaMgr = $_POST["MFAreaMgr"];
$SCContact = $_POST["SCContact"];
$VMAttendee = $_POST["VMAttendee"];
$ConMgr = $_POST["ConMgr"];
$SiteSuper = $_POST["SiteSuper"];
$SiteSuperAsst = $_POST["SiteSuperAsst"];
$VIPOpenInvite = $_POST["VIPOpenInvite"];
$CCCLunchInvite = $_POST["CCCLunchInvite"];
...
$n = count($rowData);
$i = 0;
while ($i < $n)
{
$query2 = "UPDATE communities SET HOContact='" . $HOContact[$i] . "',HOAcctContact='" . $HOAcctContact[$i] .
"',MUAreaMgr='" . $MUAreaMgr[$i] . "',StAreaMgr='" . $StAreaMgr[$i] . "',MFAreaMgr='" . $MFAreaMgr[$i] . "',SCContact='" . $SCContact[$i] .
"',VMAttendee='" . $VMAtttendee[$i] . "',ConMgr='" . $ConMgr[$i] . "',SiteSuper='" . $SiteSuper[$i] . "',SiteSuperAsst='" . $SiteSuperAsst[$i] .
"',VIPOpenInvite='" . $VIPOpenInvite[$i] . "',CCCLunchInvite='" . $CCCLunchInvite[$i] . "' WHERE CName=" . $CName[$i] . " AND ContactID=" . $id;
echo("Exisiting record SQL for Communities Table: " . $query2 . "<br><br>");
$i++;
}I think the easiest thing might be to set the off flag for the check boxes not selected, but I'm having trouble figuring that out.
Or maybe there is a completely different way to do it...
Thanks for you time.
Wade