Click to See Complete Forum and Search --> : Adding Ability To Edit Multiple Records At One Time


msmith29063
03-02-2004, 12:53 PM
What is the best way to add the ability to edit the field called equipPrice for multiple records and click the Update button? Note the Update button is being used for deleting checked items as well. Thank you so much for any help/advice.

<?php

// Connect to database
...

// If the form was submitted, delete the items selected from the database
if ($_POST["deleted_items"]) {

$deleted_items = join(', ', $_POST["deleted_items"]);
$query1 = "DELETE FROM equipment WHERE $equipID IN ($deleted_items)";
$result1 = @mysql_query($query1);
header("Location: index.php");
}

// Build query to fetch items from database
$query2 = "SELECT * FROM equipment ORDER BY $sortOrder";

// Execute query
$result2 = @mysql_query($query2);

// If query was okay AND we have at least 1 item...
if ($result2 && @mysql_num_rows($result2) > 0) {

print "<form action=\"index.php\" method=\"post\" target=\"main\">\n";
print "<table border=\"0\" cellspacing=\"0\" cellpadding=\"8\">\n";

// For each item returned from query...
while($row2 = mysql_fetch_array($result2)) {

print "<tr bgcolor=\"$row_color\">\n";
print "<td align=\"center\" valign=\"middle\"><input type=\"checkbox\" name=\"deleted_items[]\" value=\"" . stripslashes($row2['equipID']) . "\"></td>\n";

...

print "<td class=\"tiny\" align=\"left\" valign=\"middle\" nowrap>$<input type=\"text\" name=\"equipPrice\" value=\"" . stripslashes($row2['equipPrice']) . "\" size=\"8\"></td>\n";
print "</tr>\n";
}

print "</table>\n";
print "<br>\n";
print "<input type=\"submit\" value=\"Update\">\n";
print "</form>\n";

} else {

...

// Close link to MySQL server
mysql_close($link);

?>

crh3675
03-02-2004, 01:12 PM
Add the record ID to the text box name you are displaying:



print "<td class=\"tiny\" align=\"left\" valign=\"middle\" nowrap>$<input type=\"text\" name=\"equipPrice_".$row2['equipID']."\" value=\"" . stripslashes($row2['equipPrice']) . "\" size=\"8\"></td>\n";
print "</tr>\n";



Then, when you pass it to your update page:



foreach($_POST as $field=>$value){
if (eregi("equipPrice",$field)){
$id=substr($field,strpos("_")+1); // Extract ID from after Underscore
$sql="update yourtable set equipPrice='$value' where equipId=$id";
mysql_query($sql);
}

msmith29063
03-03-2004, 04:31 AM
Instead of processing to itself, I've created a new page to process the updates. Neither the checkbox delete multiple items or the price field update are working correctly. Here is the PHP for the new page I've created:

...
// Update revised prices, if applicable
foreach ($_POST as $field => $value) {
if (eregi("equipPrice", $field)) {

// Extract ID from after Underscore
$id = substr($field, strpos("_")+1);

// Build query to revise items in database
$query1 = "UPDATE equipment SET equipPrice='$value' WHERE equipID=$id";

// Execute query
$result1 = @mysql_query($query1);
}
}

// Delete the items selected, if any, from the database
if ($_POST["deletedItems"]) {

$deletedItems = implode(",", $_POST["deletedItems"]);

// Build query to delete items from database
$query2 = "DELETE FROM equipment WHERE $equipID IN ($deletedItems)";

// Execute query
$result2 = @mysql_query($query2);
}

// If either or both queries are okay...
if ($result1 || $result2) {

print "Equipment successfully updated<br><br><a href=\"displayequipment.php\" target=\"main\">Go back</a>";

} else {

// Report error
print "Could not update equipment<br><br><a href=\"displayequipment.php\" target=\"main\">Go back</a>";
}
...

Here are snippets from the form page:

...
<input type=\"checkbox\" name=\"deletedItems[]\" value=\"" . stripslashes($row4['equipID']) . "\">
...

...
<input type=\"text\" name=\"equipPrice_" . $row4['equipID'] . "\" value=\"" . stripslashes($row4['equipPrice']) . "\" size=\"8\">
...

Any help would be greatly appreciated. Thank you so much for your time.