April 4th, 2013, 04:57 PM
Pdo update checkboxlist in mysql
Ok, hopefully I can explain where I am stuck.
Here is my situation:
I have a mysql database and 3 tables.
- contact (containing a contact_id and more)
- event_manager (containing event_id and more)
- contact_events (containing contact_id and event_id)
When I insert a new contact using php pdo, and I check several checkboxes in the checkboxlist I get the value from $_POST['eventlist'], than I take the contact_id (just created) and add it to the contact_events table linked to an event_id. (every event is on a new row).
conact_id - event_id
The insert works.
But now when I want to update the contact and change several of the checkboxes in the checkboxlist. The new values need to be added to the table, but the events not checked anymore need to be removed from the table (meaning the whole row).
I know that when I use $_POST only the checked checkboxes are being passed but I have no idea on how to remove unchecked events.
Does anyone has an idea on how to solve this.
Please help me with an example.
April 4th, 2013, 05:26 PM
Most common: delete every relationship and create the new ones anew. A bit wasteful but very easy to implement (literally you just add a DELETE query).
Smarter: use hidden inputs to know which checkboxes weren't checked.
When unchecked you'll still receive the hidden field and when checked it will overwrite the hidden's value. Then you can loop through and delete the ones with a value=0.
<input type="hidden" name="event_id" value="0" />
<input type="checkbox" name="event_id" value="1" />
April 5th, 2013, 01:20 AM
When I would use the hidden field, is then the value (0, 1), the value that will be added to the database?
Originally Posted by requinix
Because that is not what I need.
At the looks of the two methods, I think I prefer the first one.
April 5th, 2013, 03:57 AM
The actual value doesn't matter - all it tells you is whether the checkbox was checked or not. What you do with that information is up to you.