Page 1 of 1

updating multiple mysql rows

Posted: Thu May 19, 2005 1:18 pm
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.

Posted: Thu May 19, 2005 1:26 pm
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]

Posted: Fri May 20, 2005 3:05 am
by Tubby
Hi shiznatix, could you explain that a bit further?

Posted: Fri May 20, 2005 3:31 am
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.

Posted: Fri May 20, 2005 4:10 am
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?

Posted: Sat May 28, 2005 8:32 am
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