Updating Multiple Rows in MySQL using PHP

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 mysql_connect("localhost","root",""); mysql_select_db("students") or die("Unable to select database"); $sql = "SELECT * FROM students ORDER BY id"; $result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error()); $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"; while ($students = mysql_fetch_array($result)) { 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 mysql_connect("localhost","root",""); mysql_select_db("students") or die("Unable to select database"); $size = count($_POST['address']); $i = 0; while ($i < $size) { $address= $_POST['address'][$i]; $id = $_POST['id'][$i]; $query = "UPDATE students SET address = '$address' WHERE id = '$id' LIMIT 1"; mysql_query($query) or die ("Error in query: $query"); echo "$address<br /><br /><em>Updated!</em><br /><br />"; ++$i; } ?>
Import the SQL Statement into your database called "students" using PHPMyAdmin included in the zip file.
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments
great
thanks alot sir for this solution. It has been a problem on my mind for quite some time now. Another big problem i have is how to create a line graph using data from a database such that when the database is altered, the graph automatically updates itself. if you can offer any help at all, please do so by sending me a mail via ; edemnartey@gmail.com. Thank you.
pls. help me!!! :((
can you create a directory system using php? pls.. i need this. my prof. give us a code does not working thats why dnt know how to do that system. so pls. can you create a directory system. any directory like telephone directory, employee directory etc. anything that you can do. pls. help me. :((
Thank you
Hi sir,,hahaha,,,,thank you gd,,,na kwa ko na kag na try ko na man butang sakun system,,lamat gd nga madamu,,hahaha,,uhm,,,,ang prob nlg is that,on how to add regular load to a specific students,,,rehus sg enrollment system,ke sa sbg ang system ko ga add course individualy,,,t,,it realy takes tym for the admin to input such data,,hmmpt,,ask ko tne sir panu mag add regular load sg students para mpahapus ang ubra sg admin or the registrar..pa check ko cmu sir if mag balik ko sa skul...kag thank you gd liwat sa codes...hahah....,,,,by:Smool Eztremos
THX
Thanks a lot, this was very helpful!
THK a lot
As you wrote there is no many tutorials on this subject,
helped me very much.
Add new comment