Updating multiple row with multiple values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Wade
Forum Commoner
Posts: 41
Joined: Mon Dec 18, 2006 10:21 am
Location: Calgary, AB, Canada

Post by Wade »

Jcart wrote:

Code: Select all

<?php echo $x; ?>
:wink:
Sorry guys I am so lost now.
How does echoing the value of $x pertain to the record[$x][FIELDNAME] in the code??

:?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

In the HTML, it means volumes.
Wade
Forum Commoner
Posts: 41
Joined: Mon Dec 18, 2006 10:21 am
Location: Calgary, AB, Canada

Post by Wade »

D'oh! I can't believe I missed that...

Thanks...

:oops:
Wade
Forum Commoner
Posts: 41
Joined: Mon Dec 18, 2006 10:21 am
Location: Calgary, AB, Canada

Post by Wade »

Ok fixed up the code as follows:

Code: Select all

<?php  
$x=0;
mysql_data_seek( $result1,0);
while ($row1 = mysql_fetch_assoc($result1)) { ?>
<tr><td><input type="text" value="<?php echo $row1['CName']; ?>" name="record[<?php echo ($x); ?>][CName]" size="15" /></td>
<td><input type="checkbox" name="record[<?php echo ($x); ?>][HOContact]" <?php if($row1["HOContact"]){echo " CHECKED";} ?>  /></td>
<td><input type="checkbox" name="record[<?php echo ($x); ?>][HOAcctContact]" <?php if($row1["HOAcctContact"]){echo " CHECKED";} ?> /></td>
<td><input type="checkbox" name="record[<?php echo ($x); ?>][MUAreaMgr]" <?php if($row1["MUAreaMgr"]){echo " CHECKED";} ?> /></td>
<td><input type="checkbox" name="record[<?php echo ($x); ?>][StAreaMgr]" <?php if($row1["StAreaMgr"]){echo " CHECKED";} ?>  /></td>
<td><input type="checkbox" name="record[<?php echo ($x); ?>][MFAreaMgr]" <?php if($row1["MFAreaMgr"]){echo " CHECKED";} ?>  /></td>
<td><input type="checkbox" name="record[<?php echo ($x); ?>][SCContact]" <?php if($row1["SCContact"]){echo " CHECKED";} ?>  /></td>
<td><input type="checkbox" name="record[<?php echo ($x); ?>][VMAttendee]" <?php if($row1["VMAttendee"]){echo " CHECKED";} ?>  /></td>
<td><input type="checkbox" name="record[<?php echo ($x); ?>][ConMgr]" <?php if($row1["ConMgr"]){echo " CHECKED";} ?>  /></td>
<td><input type="checkbox" name="record[<?php echo ($x); ?>][SiteSuper]" <?php if($row1["SiteSuper"]){echo " CHECKED";} ?>  /></td>
<td><input type="checkbox" name="record[<?php echo ($x); ?>][SiteSuperAsst]" <?php if($row1["SiteSuperAsst"]){echo " CHECKED";} ?>  /></td>
<td><input type="checkbox" name="record[<?php echo ($x); ?>][VIPOpenInvite]" <?php if($row1["VIPOpenInvite"]){echo " CHECKED";} ?>  /></td>
<td><input type="checkbox" name="record[<?php echo ($x); ?>][CCCLunchInvite]" <?php if($row1["CCCLunchInvite"]){echo " CHECKED";} ?>  /></td></tr>
<?php ++$x; ?>
<?php }?>
And the save.php:

Code: Select all

$rowData = $_POST["record"];

$n        = count($rowData);
$i        = 0;
echo("rowData count is: " . $n);
while ($i < $n)
{
$query2 = "UPDATE communities SET HOContact='" . $rowData[$i]["HOContact"] . "',HOAcctContact='" . $rowData[$i]["HOAcctContact"] .
 "',MUAreaMgr='" . $rowData[$i]["MUAreaMgr"] . "',StAreaMgr='" . $rowData[$i]["StAreaMgr"] . "',MFAreaMgr='" . $rowData[$i]["MFAreaMgr"] .
 "',SCContact='" . $rowData[$i]["SCContact"] . "',VMAttendee='" . $rowData[$i]["VMAtttendee"] . "',ConMgr='" . $rowData[$i]["ConMgr"] .
 "',SiteSuper='" . $rowData[$i]["SiteSuper"] . "',SiteSuperAsst='" . $rowData[$i]["SiteSuperAsst"] .
 "',VIPOpenInvite='" . $rowData[$i]["VIPOpenInvite"] .  "',CCCLunchInvite='" . $rowData[$i]["CCCLunchInvite"] . "'  WHERE CName=" . $rowData[$i]["CName"] . " AND ContactID=" . $id;
echo("Exisiting record SQL for Communities Table: " . $query2 . "<br><br>");
$i++;
}
Which appears to pass through the right data except I'm still getting these errors:

Code: Select all

rowData count is: 4
Notice: Undefined index: HOAcctContact in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\savemisc.php on line 51

Notice: Undefined index: StAreaMgr in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\savemisc.php on line 52

Notice: Undefined index: MFAreaMgr in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\savemisc.php on line 52

Notice: Undefined index: SCContact in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\savemisc.php on line 53

Notice: Undefined index: VMAtttendee in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\savemisc.php on line 53

Notice: Undefined index: ConMgr in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\savemisc.php on line 53

Notice: Undefined index: SiteSuper in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\savemisc.php on line 54

Notice: Undefined index: SiteSuperAsst in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\savemisc.php on line 54

Notice: Undefined index: VIPOpenInvite in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\savemisc.php on line 55

Notice: Undefined index: CCCLunchInvite in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\savemisc.php on line 55

Exisiting record SQL for Communities Table: UPDATE communities SET HOContact='on',HOAcctContact='',MUAreaMgr='on',StAreaMgr='',MFAreaMgr='',SCContact='',VMAttendee='',ConMgr='',SiteSuper='',SiteSuperAsst='',VIPOpenInvite='',CCCLunchInvite='' WHERE CName=Secord AND ContactID=280
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Since you are using literal references (in the HTML) you should possibly use a foreach() instead of straight counting as the records sent may not match the numbers you attempt to use.
Wade
Forum Commoner
Posts: 41
Joined: Mon Dec 18, 2006 10:21 am
Location: Calgary, AB, Canada

Post by Wade »

Even switching it to a foreach I still get the errors (unless all checkboxes are on). I think I can live with this by changing the error reporting when we go live.

Question: Given the following code what would you suggest as the best way to check all the array elements and change them to a 1 if they are 'on or a 0 if they unset?

Code: Select all

foreach($rowData as $v)
{
$query2 = "UPDATE communities SET HOContact='" . $v["HOContact"] . "',HOAcctContact='" . $v["HOAcctContact"] .
 "',MUAreaMgr='" . $v["MUAreaMgr"] . "',StAreaMgr='" . $v["StAreaMgr"] . "',MFAreaMgr='" . $v["MFAreaMgr"] .
 "',SCContact='" . $v["SCContact"] . "',VMAttendee='" . $v["VMAttendee"] . "',ConMgr='" . $v["ConMgr"] .
 "',SiteSuper='" . $v["SiteSuper"] . "',SiteSuperAsst='" . $v["SiteSuperAsst"] .
 "',VIPOpenInvite='" . $v["VIPOpenInvite"] .  "',CCCLunchInvite='" . $v["CCCLunchInvite"] . "'  WHERE CName=" . $v["CName"] . " AND ContactID=" . $id;
echo("Exisiting record SQL for Communities Table: " . $query2 . "<br><br>");
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Yes, you will get errors blindly using the elements. You can either use a large number of isset() calls (probably coupled with ternaries) or what I would suggest is creating a blank can you can use as a template for each of the records submitted. Something like this:

Code: Select all

if( isset($_POST['rowData']) and is_array($_POST['rowData']))
{
  $blank = array(
    'HOContact' => 0,
    'HOAcctContact' => 0,
    // etc
  );
  foreach($_POST['rowData'] as $record)
  {
    $h = $blank;
    if(is_array($record))
    {
      foreach($record as $k => $v)
      {
        if(isset($blank[$k]))
        {
          $h[$k] = 1;
        }
      }
    }
    // do your query here.
  }
}
On that later comment line you can automatically generate your update query based on the data contained in $h using a foreach again. I do this all the time so I only have to update the list of fields in one location (the blank.)

edit: misplaced the comment line. :D
Last edited by feyd on Tue Dec 19, 2006 9:15 am, edited 1 time in total.
Wade
Forum Commoner
Posts: 41
Joined: Mon Dec 18, 2006 10:21 am
Location: Calgary, AB, Canada

Post by Wade »

Ok I think I have it. it looks messy but it works. if anyone has any suggestions I'd be happy to listen!!

Code: Select all

foreach($rowData as $v)
{
if($v["HOContact"]) {$v["HOContact"] = 1;} else {$v["HOContact"] = 0;}
if($v["HOAcctContact"]) {$v["HOAcctContact"] = 1;} else {$v["HOAcctContact"] = 0;}
if($v["MUAreaMgr"]) {$v["MUAreaMgr"] = 1;} else {$v["MUAreaMgr"] = 0;}
if($v["StAreaMgr"]) {$v["StAreaMgr"] = 1;} else {$v["StAreaMgr"] = 0;}
if($v["MFAreaMgr"]) {$v["MFAreaMgr"] = 1;} else {$v["MFAreaMgr"] = 0;}
if($v["SCContact"]) {$v["SCContact"] = 1;} else {$v["SCContact"] = 0;}
if($v["VMAttendee"]) {$v["VMAttendee"] = 1;} else {$v["VMAttendee"] = 0;}
if($v["ConMgr"]) {$v["ConMgr"] = 1;} else {$v["ConMgr"] = 0;}
if($v["SiteSuper"]) {$v["SiteSuper"] = 1;} else {$v["SiteSuper"] = 0;}
if($v["SiteSuperAsst"]) {$v["SiteSuperAsst"] = 1;} else {$v["SiteSuperAsst"] = 0;}
if($v["VIPOpenInvite"]) {$v["VIPOpenInvite"] = 1;} else {$v["VIPOpenInvite"] = 0;}
if($v["CCCLunchInvite"]) {$v["CCCLunchInvite"] = 1;} else {$v["CCCLunchInvite"] = 0;}

$query2 = "UPDATE communities SET HOContact='" . $v["HOContact"] . "',HOAcctContact='" . $v["HOAcctContact"] .
 "',MUAreaMgr='" . $v["MUAreaMgr"] . "',StAreaMgr='" . $v["StAreaMgr"] . "',MFAreaMgr='" . $v["MFAreaMgr"] .
 "',SCContact='" . $v["SCContact"] . "',VMAttendee='" . $v["VMAttendee"] . "',ConMgr='" . $v["ConMgr"] .
 "',SiteSuper='" . $v["SiteSuper"] . "',SiteSuperAsst='" . $v["SiteSuperAsst"] .
 "',VIPOpenInvite='" . $v["VIPOpenInvite"] .  "',CCCLunchInvite='" . $v["CCCLunchInvite"] . "'  WHERE CName=" . $v["CName"] . " AND ContactID=" . $id;
echo("Exisiting record SQL for Communities Table: " . $query2 . "<br><br>");
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Wade wrote:if anyone has any suggestions I'd be happy to listen!!
feyd just gave you a very help suggestion, as well as some code..
Wade
Forum Commoner
Posts: 41
Joined: Mon Dec 18, 2006 10:21 am
Location: Calgary, AB, Canada

Post by Wade »

I saw that, I had just posted the comment 1 sec before getting the notification.

Thanks feyd!!
Post Reply