ColdFusion & MSSQL : how to insert multiple rows with one unique id in one submission

前端 未结 2 865
难免孤独
难免孤独 2021-01-19 05:35

can anyone help me on how to submit multiple rows in one submission?

this survey form will display a set of skill that derived from table skill. the teacher will hav

相关标签:
2条回答
  • 2021-01-19 06:10

    Another simple approach for inserting multiple records is an INSERT .. SELECT. (It is also mentioned in the link banyr posted). Because the skill id's are stored in another table, you can use an IN clause to SELECT them. Then insert those values directly into your other table studenSkill with a simple query, no looping.

    INSERT INTO studenSkill ( studenId, skillId )
    SELECT <cfqueryparam value="#form.studentId#" cfsqltype="cf_sql_integer">
           , skillId
    FROM   skill
    WHERE  skillId IN 
           (
           <cfqueryparam value="#form.skillId#" cfsqltype="cf_sql_integer" list="true">
           )
    



        <input type="checkbox" name="skillid" value="skillid" checked="checked">
    

    BTW, in case that is not a typo, do not forget the # signs around the query column name ie "skillid"

      <input type="checkbox" name="skillid" value="#skillid#" checked="checked">
    
    0 讨论(0)
  • 2021-01-19 06:25

    You should be able to use the following syntax, assuming you're on SQL server 2008:

    INSERT INTO StudentSkill (StudentID, SkillID)
    VALUES (100, 1), (100, 2), (100, 3)
    

    This method gleaned from here, which also contains a couple of alternative methods.

    You'll just need to iterate over the list of IDs in FORM.skillid (assuming that's how your form works) to build up the SQL above. Also, make sure you use <CFQueryParam> on the values when you build up the SQL. Something like the code below ought to do:

    <cfif ListLen(FORM.skillid)>
      <cfquery>
       INSERT INTO StudentSkill (StudentID, SkillID)
        VALUES
       <cfloop list="#form.skillid#" index="skill">
         (<cfqueryparam value="#form.studentID#" CFSQLType="CF_SQL_INTEGER">, 
           <cfqueryparam value="#skill#" CFSQLType="CF_SQL_INTEGER">)
       </cfloop>
      </cfquery>
    </cfif>
    
    0 讨论(0)
提交回复
热议问题