Skip to main content
Known Participant
November 28, 2008
Question

Checkboxes and INSERT/UPDATE - help!!

  • November 28, 2008
  • 1 reply
  • 331 views
I have 3 tables (locations, categories, location_categories) I need to do an update and insert. Here are the three selects which retrieves the checked categories. How can I do an insert / update into the locations_categories table creating a record for each checkbox that is checked. I know I need to do a loop on a list but not sure how to write it. Here are my select statements.

<cfquery name="getlocations">
SELECT locationid, locname
FROM locations
</cfquery>

<cfquery name="getcategories">
SELECT categoryid, catname
FROM categories
</cfquery>

<cfquery name="loccatselected">
SELECT
categoryid
FROM location_categories
WHERE locationid=#URL.locationid#
</cfquery>
<cfset variables.selectedCategories=ValueList(loccatselected.categoryid)>
    This topic has been closed for replies.

    1 reply

    Inspiring
    November 28, 2008
    Two approaches are

    1) Delete all existing records for each location and then re-insert all checked values
    2) Compare the ids and figure out what has changed.
    - Delete items that were originally checked, but are now unchecked.
    - Insert new items that were originally unchecked, but are now checked.
    - Do nothing with items that have not changed

    The first approach is simpler. But usually results in some unnecessary deletes. The second approach is more focused. But the logic is slightly more complicated.