title: Coldfusion SQL Insert Loop
body: <p>Got stuck with an issue and thought I might see if anyone had any ideas on how to fix it.</p>
  
  <p>Basically, I pass in multiple values under a singular variable, and I want to use a loop to extract each individual value and insert it at the same time.</p>
  
  <p>For example, ischecked is the variable I use to pass in device values.  If I were to select two devices, press submit and dump the variable #form.ischecked# in my processing page, I would get a value that says 41,42 for example.  I need a way to split these values up, and figured a cfloop and insert would be perfect for that.  </p>
  
  <p>This is all done in a cfc, if that matters.  </p>
  
  <pre><code>        &lt;cfset devicearray = ArrayNew(1)&gt;
          &lt;cfset temp = ArrayAppend(devicearray, #ischecked#)&gt;
          &lt;cfset test = ArrayToList(devicearray, ",")&gt;
          &lt;cfset length= ListLen(test)&gt;\
          \\this loop takes the amount of devices selected, and outputs the length of the list. 
  </code></pre>
  
  <p>I use this to find out how long the insert loop should go for. 
   I could have also just checked the length of the array originally, but I was going to use the list for another purpose as well.</p>
  
  <pre><code>    &lt;cfset devicetest = #form.ischecked#&gt;
  
          &lt;cfset usertest = #form.userid#&gt;
  
          \\form.ischecked is the variable that contains the device IDs
  
          \\form.userid is the variable that contains the User IDs
  
          &lt;cfquery name="loopquery" datasource="Test"&gt;
          &lt;cfloop from="1" to="#length#" index="i"&gt;
  
          \\loop from 1 to "length", the number of Devices selected as specified earlier
  
          INSERT INTO Loan (DeviceID, UserID)
          VALUES ("#Evaluate("devicetest#i#")#","#Evaluate("userID#i#")#" )
          &lt;/cfloop&gt;
          &lt;/cfquery&gt;
  </code></pre>
  
  <p>So basically that's where I'm stuck at, the loop goes over the values but it looks for devicetest1 instead of device test (because of the index), but I can't for the life of me figure out how to pass in the values so that it picks out each one individually.</p>
  
  <p>I've seen some examples where people have appended the index (i) with the value, then used it to insert, but didn't really understand how it would have worked.  </p>
  
  <p>Thanks,
  Jordan</p>