#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    230
    Rep Power
    8

    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).

    For example:

    conact_id - event_id
    1 3
    1 7
    1 11
    1 15

    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.

    Thanks
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    Two methods:

    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.
    Code:
    <input type="hidden" name="event_id[3]" value="0" />
    <input type="checkbox" name="event_id[3]" value="1" />
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    230
    Rep Power
    8
    Originally Posted by requinix
    Two methods:

    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.
    Code:
    <input type="hidden" name="event_id[3]" value="0" />
    <input type="checkbox" name="event_id[3]" value="1" />
    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.
    When I would use the hidden field, is then the value (0, 1), the value that will be added to the database?
    Because that is not what I need.

    At the looks of the two methods, I think I prefer the first one.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    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.

IMN logo majestic logo threadwatch logo seochat tools logo