...

View Full Version : Updating Multiple Rows



dprichard
07-16-2007, 04:35 PM
I am trying to update multiple rows with one submit button.

I currently have a table that repeats out the following form for each folder in the database:


<form name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<label>
<div align="center">
<input name="updateorder" type="hidden" id="updateorder" value="updateorder">
<input type="hidden" name="dcid" id="dcid" value="<?php echo $row_categories['dcid']; ?>">
<input type="hidden" name="dcparent" id="dcparent" value="<?php echo $row_categories['dcparent']; ?>">
<input name="dcorder" type="text" id="dcorder" size="3" maxlength="3" value="<?php echo $row_categories['dcorder']; ?>">
<input name="submit2" type="image" src="/images/refresh.png" alt="Refresh Item" align="middle" width="16" height="16">
</div>
</label>
<div align="center"></div>
</form>

Then I use this update statement to update the database.


if(isset($_POST['updateorder'])) {
$dcorder = mysql_real_escape_string($_POST['dcorder']);
$dcid = mysql_real_escape_string($_POST['dcid']);
$updateorder = mysql_query("UPDATE documentcategories SET dcorder='$dcorder' WHERE dcid='$dcid'") or die(mysql_error());
}

I would like to be able to encompass the entire table and update them with one button when submitted instead of having to update one box, then submit and the next box then submit.

Any help would be greatly appreciated!!!

NancyJ
07-16-2007, 06:16 PM
You can use form field arrays.

eg.
you name your fields
name = "data[<?=$row_categories['dcid']?>][nameoffield]"

then when you submit the form you'll get data like



data => array(
1 => array(
dcparent => dcparent_value
dcorder => dcorder_value
)
2=> array(
dcparent => dcparent_value
dcorder => dcorder_value
)
)


Then you would do something like this :



if(isset($_POST['updateorder'])) {
foreach($_POST['data'] as $id => $array)
{

$dcorder = mysql_real_escape_string($array['dcorder']);
$id = mysql_real_escape_string($id);
$updateorder = mysql_query("UPDATE documentcategories SET dcorder='$dcorder' WHERE dcid='$id'") or die(mysql_error());
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum