/    Sign up×
Community /Pin to ProfileBookmark

Adding Ability To Edit Multiple Records At One Time

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);

?>

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@crh3675Mar 02.2004 — Add the record ID to the text box name you are displaying:

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



Then, when you pass it to your update page:

<i>
</i>
foreach($_POST as $field=&gt;$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);
}

Copy linkTweet thisAlerts:
@msmith29063authorMar 03.2004 — 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.
×

Success!

Help @msmith29063 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...