I have an html form build from a MySQL table. The mysql table has 2 fields, itemnumber and status. Status is either 1 or 0. I do not know how many results may returned at any given time. In the sample below I show 4, but it could be any number of pairs.

The results are displayed using radio buttons on the html page. In the example below, x indicates it is checked. A check in the yes column means the mysql record had a value of 1 an no x (unchecked) means the value was 0.

Item Yes No

3 x
6 x
7 x

9 x

Up to this point I have no problem.

Now, the form is to be submitted and the status of the radio buttons must get updated in the mysql table. This is where I am stuck. I need some sort of for loop combined with an update command, but aren'r sure on what I am looping.

I have named each radio button form element by its item number. So, when I submit the form the following php variables are passed.

$3=1, $6=0, $7=0, $9=1

If I only had to update one record, say item 3, I would use the following mysql query .

"UPDATE tablename SET status = '$3' WHERE itemnumber = '3'"

But, I don't know what the items are or how many there are. I need to loop thru and substitute the 3 in the above update query with the correct value and do so for how ever many pairs there are with that paticular submission.

Any advise would be most appreciated.

Thank you,
-Bob

    The way I do this is through a combination of hidden form inputs.

    You need to keep a counter of the number of elements while you're outputting them:

    <input type=hidden name=Counter value=$Counter>

    rather than naming the radio buttons after their id, name them with $Counter:

    <input type=radio name=Radio$Counter>

    And you need to link them to their database values, so store that in hidden variables also named using $Counter.

    <input type=hideen name=Value$Counter value=$database_id>

    After the form is submitted, you can do a for loop from 1 to $Counter. In each iteration of the loop you can then update each record.

    A downfall of this method is that you do have to update each record, even if they haven't been modified. You could add another hidden input called modified$Counter and update it's value with JavaScript if the user changes the radio buttons, but then you're dependant on the JavaScript, which is probably not a good thing.

      Write a Reply...