There are so many tutorials on how to update one (1) record at a time. But only few tutorial on how to update multiple rows at once.
I dedicate this tutorial to my 3rd year students.
Save the following code as select.php:
- <?php
- $sql = "SELECT * FROM students ORDER BY id";
- $i = 0;
- echo '<table width="50%">';
- echo '<tr>';
- echo '<td>ID</td>';
- echo '<td>Name</td>';
- echo '<td>Address</td>';
- echo '</tr>';
- echo "<form name='form_update' method='post' action='update.php'>\n";
- echo '<tr>';
- echo "<td>{$students['id']}<input type='hidden' name='id[$i]' value='{$students['id']}' /></td>";
- echo "<td>{$students['name']}</td>";
- echo "<td><input type='text' size='40' name='address[$i]' value='{$students['address']}' /></td>";
- echo '</tr>';
- ++$i;
- }
- echo '<tr>';
- echo "<td><input type='submit' value='submit' /></td>";
- echo '</tr>';
- echo "</form>";
- echo '</table>';
- ?>
Save the following code as update.php:
- <?php
- $i = 0;
- while ($i < $size) {
- $address= $_POST['address'][$i];
- $id = $_POST['id'][$i];
- $query = "UPDATE students SET address = '$address' WHERE id = '$id' LIMIT 1";
- echo "$address<br /><br /><em>Updated!</em><br /><br />";
- ++$i;
- }
- ?>
0 comments:
Post a Comment