Updating Multiple Rows in MySQL using PHP

Language
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:
  1. <?php
  2. mysql_connect("localhost","root","");
  3. mysql_select_db("students") or die("Unable to select database");
  4.  
  5. $sql = "SELECT * FROM students ORDER BY id";
  6.  
  7. $result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
  8.  
  9. $i = 0;
  10.  
  11. echo '<table width="50%">';
  12. echo '<tr>';
  13. echo '<td>ID</td>';
  14. echo '<td>Name</td>';
  15. echo '<td>Address</td>';
  16. echo '</tr>';
  17.  
  18. echo "<form name='form_update' method='post' action='update.php'>\n";
  19. while ($students = mysql_fetch_array($result)) {
  20.         echo '<tr>';
  21.         echo "<td>{$students['id']}<input type='hidden' name='id[$i]' value='{$students['id']}' /></td>";
  22.         echo "<td>{$students['name']}</td>";
  23.         echo "<td><input type='text' size='40' name='address[$i]' value='{$students['address']}' /></td>";
  24.         echo '</tr>';
  25.         ++$i;
  26. }
  27. echo '<tr>';
  28. echo "<td><input type='submit' value='submit' /></td>";
  29. echo '</tr>';
  30. echo "</form>";
  31. echo '</table>';
  32. ?>
Save the following code as update.php:
  1. <?php
  2. mysql_connect("localhost","root","");
  3. mysql_select_db("students") or die("Unable to select database");
  4.  
  5. $size = count($_POST['address']);
  6.  
  7. $i = 0;
  8. while ($i < $size) {
  9.         $address= $_POST['address'][$i];
  10.         $id = $_POST['id'][$i];
  11.        
  12.         $query = "UPDATE students SET address = '$address' WHERE id = '$id' LIMIT 1";
  13.         mysql_query($query) or die ("Error in query: $query");
  14.         echo "$address<br /><br /><em>Updated!</em><br /><br />";
  15.         ++$i;
  16. }
  17. ?>
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

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 ; [email protected]. Thank you.

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. :((

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

Thanks a lot, this was very helpful!

As you wrote there is no many tutorials on this subject, helped me very much.

Sir, U have helped me a lot..I was really looking for the this for the last one week..Ur code is simple and very very dramatic ...Lot of thanks,

Thanks a lot !!! Sir you have helped me a lot.

It has been very useful and with a clear code, thank you !!!!!

Thank you very much, your code helped me a lot, thanks.

sir, can i change the text box which is going to be updated to combo box with my own option values means i want combo box in place of text box,. with the pre filled option and some some my provided options

It very nice code dear

Hi, Thanks for the great code. How does it work with SELECT fields? When I fill the SELECT field with table data, I don't know how to pass the selected value to the processing page. I get there a blank value Thanks again!

I found this site really great. Giving solutions without any pomp and shows which other websites actually doing. Great! cannot getting appropriate word to praise

Add new comment