Update multiple selects

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default Update multiple selects

    I have a list box on a form that allows for multiple selections. The selections
    are dumps into multiple rows and are tied into another table thru a foreign
    key. I need to find out how to update the table with the multiple rows.

    Scenario: We have an online application that allows individuals to sign up as
    volunteers and they have an option of selecting a preferred school(s) from a
    list box. If the volunteer at one point selected 3 preferred schools but would
    like to go back and delete one and add 2 different ones.

    Below is how I handled the original insert for the multiple selections.

    ANY assistance will be greatly appreciated. Thanks!



    <cfset strPrefList = "#FORM.PREFSCHOOL#">
    <cfparam name="strPrefList" default="0">

    <!-- Start to loop thru list of chosen PrefSchools -->
    <cfloop index="listindex" list="#strPrefList" delimiters=",">

    <cfquery name="InsertPrefSchool" dbtype="odbc" datasource="dsn">
    INSERT INTO VolPrefSchool(VOLIDFK,PREFSCHOOL)
    VALUES(#qGetRegID.NEWID#,#listindex#)
    </cfquery>

    </cfloop>

    cfquest Guest

  2. Similar Questions and Discussions

    1. update multiple records in multiple tables from one form
      hello I have been trying to run multiple update queries based on the data entered by user. Brief background: I am fetching data from various...
    2. CFselect with multiple selects
      I have a form that collects a bunch of information. one of the form feilds is a select that allows the user to choose multiple languages. The form...
    3. Dynamically popualte multiple selects
      Anyone know of Tag or something that will give you multiple dynamic selects. EX . Slecect a state and all cities in that state show up in second...
    4. Link to update page selects wrong record
      I built search and results pages that work fine but when i try to link from the results page record to an update page that i built using the Record...
    5. processing form with multiple selects
      Hello, I have a form with multiple selects (pullDown). All selects have the same name but each select has a different value. The user selects a...
  3. #2

    Default Re: Update multiple selects

    There are few ways to do this. The simplest method would probably be to first
    DELETE all of the volunteer's previous selections from the table and then
    INSERT their new selections.

    Another method would be to store the original selections in a hidden form
    field and then on the action page, use the "original" and "new" selection lists
    to DELETE any items that were de-selected and INSERT items that were newly
    selected. This method would probably be more efficient.


    mxstu Guest

  4. #3

    Default Re: Update multiple selects

    I do know how to delete from a table but I'm not sure how to do the second
    suggestion. Since it is more efficient, that is the suggestion I would rather
    go with. Can you or someone provide an example on how I might lay that one
    out? Thanks

    cfquest Guest

  5. #4

    Default Re: Update multiple selects

    (This example needs some work as I excluded some validation, CFTRANSACTION,
    CFQUERYPARAM, etc for clarity)

    Assuming you pass three variables to the action page:

    form.volunteerID
    form.newSchools (comma delimited list of numeric school IDs)
    form.originalSchools (comma delimited list of original school IDs)








    <cfparam name="form.newSchools" default="">
    <cfparam name="form.originalSchools" default="">

    <!--- A) you could put the list code in a UDF and simplify this page --->

    <!--- get schools that were de-selected--->
    <cfset removeSchools = "">
    <cfloop list="#form.originalSchools#" index="schoolID">
    <cfif ListFind(form.newSchools, schoolID) EQ 0>
    <cfset removeSchools = ListAppend(removeSchools, schoolID)>
    </cfif>
    </cfloop>

    <!--- get schools that were newly added --->
    <cfset addSchools = "">
    <cfloop list="#form.newSchools#" index="schoolID">
    <cfif ListFind(form.originalSchools, schoolID) EQ 0>
    <cfset addSchools = ListAppend(addSchools, schoolID)>
    </cfif>
    </cfloop>

    <!--- B) should also use CFQUERYPARAM --->
    <!--- C) should wrap both queries within CFTRANSACTION --->
    <!--- so if one query fails, both will fail --->
    <cfif ListLen(removeSchools) GT 0>
    <cfquery name="removeSchools" datasource="#yourDSN#">
    DELETE FROM PREFSCHOOL
    WHERE VolunteerID = #form.volunteerID# AND
    SchoolID IN (#removeSchools#)
    </cfquery>
    </cfif>

    <cfif ListLen(addSchools) GT 0>
    <cfloop list="#addSchools#" index="schoolID">
    <cfquery name="addSchools" datasource="#yourDSN#">
    INSERT INTO PREFSCHOOL (VolunteerID, SchoolID)
    VALUES (#form.volunteerID#, #schoolID#)
    </cfquery>
    </cfloop>
    </cfif>

    mxstu Guest

  6. #5

    Default Re: Update multiple selects

    Would this still work if I was using a list box on the update page that has the
    prior selections preselected? In other words, the list box is named
    #form.prefSchool# on the update page and that is passed to the action page.

    cfquest Guest

  7. #6

    Default Re: Update multiple selects

    Yes. Your field "form.prefSchool" would correspond to "form.newSchools" in the
    sample code. Just make sure you also pass a comma delimited list of the prior
    selections in a hidden form field. In the example, the prior selections are
    passed in a field called "form.originalSchools".

    Note: Make sure to change the query column names as well. I changed them for
    clarity.



    mxstu Guest

  8. #7

    Default Re: Update multiple selects

    I am not all too familiar with CFQUERYPARAM but I have looked at sample syntax. Should I place the CFQUERYPARAM in the <cfif> statement for the removeSchools or addSchools (in your example)?
    cfquest Guest

  9. #8

    Default Re: Update multiple selects

    MM recommends that you use it everywhere that you pass a parameter (ex.
    #form.myFieldName#..) to a CFQUERY. So it should be used for every form
    variable used in the CFQUERY ... DELETE and ... INSERT statements.



    mxstu Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139