Skip to main content
May 14, 2008
Answered

How do I split up form values from cfselect that allows multiple?

  • May 14, 2008
  • 3 replies
  • 839 views
I have a basic form with one drop down box, and a cfselect box that allows the user to select multiples. For each item selected in the cfselect box, I need to populate a row in the database. How I do I "split" out the values from the cfselect and populate multiple rows in the database?
    This topic has been closed for replies.
    Correct answer
    Found a great sample.. and it works great now.. http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_17007

    <cfloop index="SubSectionID" list="#Form.SubSectionID#">
    <cfquery name="InsertContentLinks" datasource="xxx">
    Insert INTO tContentLinks (ContentID, SubSectionID)
    VALUES ('#Form.ContentID#', '#SubSectionID#')
    </cfquery>
    </cfloop>

    3 replies

    Correct answer
    May 14, 2008
    Found a great sample.. and it works great now.. http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_17007

    <cfloop index="SubSectionID" list="#Form.SubSectionID#">
    <cfquery name="InsertContentLinks" datasource="xxx">
    Insert INTO tContentLinks (ContentID, SubSectionID)
    VALUES ('#Form.ContentID#', '#SubSectionID#')
    </cfquery>
    </cfloop>
    February 25, 2010

    now take that sample and optomize it a bit...

    move the query tags outside the loop and run all the inserts in once single trip to the DB.

    Inspiring
    May 14, 2008
    You have the right idea but got a detail wrong.

    Your cfloop tag has index="i" but you don't use it in your query anywhere.
    Inspiring
    May 14, 2008
    Step 1 - select more than one item and submit the form.
    Step 2 - cfdump your form.

    See anything useful?
    May 14, 2008
    I thought it was more along the lines of a loop... I tried the code below, but keep getting this error if I select multiple values from the SubSectionID Dropdown Box. I do not get the error if I just select one value from the dropdown, and the insert works fine. Argh...

    [Macromedia][SQLServer JDBC Driver][SQLServer]Syntax error converting the varchar value '1,2,3' to a column of data type int.


    <cfloop list="#Form.SubSectionID#" index="i">
    <cfquery name="InsertContentLinks" datasource="xxx">
    Insert INTO tContentLinks (ContentID, SubSectionID)
    VALUES ('#Form.ContentID#', '#Form.SubSectionID#')
    </cfquery>
    </cfloop>