updating multiple mysql rows

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Tubby
Forum Commoner
Posts: 28
Joined: Thu Oct 17, 2002 5:55 pm

updating multiple mysql rows

Post by Tubby »

Hi all,
A table in my database looks something like this:

Code: Select all

id   |   sec   |  name
-----------------------
1    |    1    |  blah
3    |    2    |  blah324
4    |    3    |  blah32
5    |    6    |  blah234
6    |    4    |  blah567
11   |    5    |  blah567567
12   |    8    |  blah35
13   |    7    |  blah46
18   |    9    |  blah456
23   |   10    |  blah43
I want the user to beable to modify the value "sec" by doing something like this:
  • Display a form which lists all the $row[name]'s and next to it in a textbox the $row[sec] :-
    • blah <1>
      blah324 <2>
      blah32 <3> ...and so on.
    When this form is submitted it will then update all the rows with the new "sec" value for that "id"
Rows may be added to the table or random rows may be removed at any time, so I can't just hard code a query to update each row.

How can this be done simply and efficiently?
Thanks.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

display some code but if you just did like
<input type="text" name="seconds[]">

then on the updating page do

Code: Select all

foreach($_POST[seconds] as $key => $val)
{
    //update the database
}
or somthing simmilar to that[/list]
User avatar
Tubby
Forum Commoner
Posts: 28
Joined: Thu Oct 17, 2002 5:55 pm

Post by Tubby »

Hi shiznatix, could you explain that a bit further?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

As far as I know you can't update more then one row at a time so what you need to do is loop through your data for each row and execute the query. shiznatix just gave you a quick example of how you might go about this.
User avatar
Tubby
Forum Commoner
Posts: 28
Joined: Thu Oct 17, 2002 5:55 pm

Post by Tubby »

Yes, but in that example how will it know which new `sec` value goes with which row?

Surely the "name" for each textbox needs to be the `id` of the row it corrisponds with, but if you do that, how would you do a while/foreach loop for updating?
User avatar
Tubby
Forum Commoner
Posts: 28
Joined: Thu Oct 17, 2002 5:55 pm

Post by Tubby »

shiznatix wrote:display some code but if you just did like
<input type="text" name="seconds[]">

then on the updating page do

Code: Select all

foreach($_POST[seconds] as $key => $val)
{
    //update the database
}
or somthing simmilar to that[/list]
Thanks, that worked with a tiny bit of modification to the form I'd created :D
Post Reply