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 $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; } ?>
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
sanjay sharan (not verified)
Tue, 07/19/2016 - 09:48
Permalink
great web site and tutorial
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
Amir Weinberger (not verified)
Wed, 06/25/2014 - 17:46
Permalink
Great Code - how does it work with SELECT fields?
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!
Evans Mwambije (not verified)
Fri, 09/20/2013 - 23:39
Permalink
Nice code
It very nice code dear
prem_123 (not verified)
Wed, 08/07/2013 - 12:03
Permalink
text box to combo box
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
Anonymous (not verified)
Sun, 06/09/2013 - 05:49
Permalink
Thank you very much
Thank you very much, your code helped me a lot, thanks.
Anonymous (not verified)
Tue, 05/07/2013 - 23:15
Permalink
Thank you
It has been very useful and with a clear code, thank you !!!!!
Anonymous (not verified)
Tue, 03/12/2013 - 05:38
Permalink
Thanks
Thanks a lot !!! Sir you have helped me a lot.
Anonymous (not verified)
Tue, 01/22/2013 - 14:25
Permalink
Thanks
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,
beni (not verified)
Fri, 02/03/2012 - 03:55
Permalink
THK a lot
As you wrote there is no many tutorials on this subject,
helped me very much.
Anonymous (not verified)
Wed, 11/23/2011 - 05:11
Permalink
THX
Thanks a lot, this was very helpful!
Anonymous (not verified)
Wed, 10/05/2011 - 04:08
Permalink
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
Anonymous (not verified)
Tue, 10/04/2011 - 13:39
Permalink
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. :((
Anonymous (not verified)
Tue, 10/04/2011 - 09:33
Permalink
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 ; [email protected]. Thank you.
Add new comment