View Full Version : checkboxes (checked or not) and MySql - update multiple rows

08-21-2007, 06:42 PM

I am having problems with checkboxes (checked or not) and MySql - update multiple rows.

My aim is to have a user update multiple records based on the status of the checkbox.

The story so far...

I am using a do-while loop to generate a list of 'items' from my db.
Each 'item' has a checkbox next to it, with a value of item_id.

The database has a 'published' field (enum 'y','n'). When published='y' the item will appear on the site and conversely published='n' wont.

When the form is submitted the checkboxes make up the published[] array.

This is where I need to update each record in the database with either a 'y' or a 'n'.

It seems that by default checkboxes are not included in the $_POST superglobal unless they are checked, I need all the items in my array, checked or not, in order to update the db.

Hope that makes sense - its been a long day...:eek:


08-21-2007, 09:09 PM
I have just had a similar problem resolved using the following query.

$query = 'UPDATE yourtable SET yourfieldf= NOW() where ops_id IN (' . implode(',', $_POST['checkbox']) . ')';

In my case I only wanted those boxes that were checked to update but I would assume (provided you have set a value for the checkbox) that the above query would work in your case.

No guarantee but this code saved me several days of heartache.


08-21-2007, 09:13 PM
You could try building an array of the items, then looping through the changes & updating the array simultaneously, and finally updating the database with the changes.