How to loop this structure and insert values to SQL table? Thanks!


I have a function which uploads files.  (Could be one file, could be many files).  I've created a structure which holds the values I *might* need to insert into a SQL table (I've tried to write this function as dynamically as possible).  I **think** I've created the structure correctly?  (But I'm new to this).

<cfset var.myFileList=arguments.myFileList>

<cfset fileInfo=StructNew()>
<cfset var.count="1">
<cfloop list="#var.myFileList#"  index="i">
<cffile
   action="upload"
   filefield="Form.#trim(i)#"
   destination="#arguments.directory##arguments.trackingNo#"
   nameconflict="#arguments.conflict#">

<cffile
  action="rename"
  source="#arguments.directory##arguments.trackingNo#\#file.serverFile#"
  destination="#arguments.directory##arguments.trackingNo#\#i#.#file.clientFileExt#"
  nameconflict="#arguments.conflict#">

<cfset var.fileName="#arguments.TrackingNo#/#i#.#File.ClientFileExt#">      
<cfset fileInfo.trackingNo[#var.count#]=arguments.trackingNo>
<cfset fileInfo.fileName[#var.count#]=var.fileName>
<cfset fileInfo.userDefinedName[#var.count#]=file.serverFile>
<cfset var.count=var.count+1>      
</cfloop>
<cfreturn fileInfo/>

In the example code above, I'm actually working with two uploads, so I need to add information about two uploads to my structure (looks like this)

struct
FILENAME struct
               1 232/myfile1.doc  
               2 232/myfile2.doc  
 
TRACKINGNO struct
              1 232  
              2 232  
 
USERDEFINEDNAME struct
            1 whippet.doc  
            2 greyhound.doc  


I can't figure out tho, how to loop this structure and insert the values into a Sql table.


<cfloop item="i" collection="#fileInfo#">
<cfquery datasource="mine">
  insert into table (fileName,trackingNo,userDefinedName,uploadDate)
  values (<cfqueryparam value = "#fileInfo.fileName#" cfsqltype="cf_sql_varchar">,
 <cfqueryparam value = "#fileInfo.trackingNo#" cfsqltype="cf_sql_integer">,
 <cfqueryparam value = "#fileInfo.userDefinedName#" cfsqltype="cf_sql_varchar">,
 <cfqueryparam value = "#now()#" cfsqltype="cf_sql_date">)
 </cfquery>
</cfloop>


whaleykAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rob_lorentzCommented:
try this.

<cfloop from="1" to="#arrayLen(fileInfo.trackingNo)#" index="x">
      <cfquery datasource="mine">
            insert into table (fileName,trackingNo,userDefinedName,uploadDate)
            values (<cfqueryparam value = "#fileInfo.fileName[x]#" cfsqltype="cf_sql_varchar">,
                  <cfqueryparam value = "#fileInfo.trackingNo[x]#" cfsqltype="cf_sql_integer">,
                  <cfqueryparam value = "#fileInfo.userDefinedName[x]#" cfsqltype="cf_sql_varchar">,
                  <cfqueryparam value = "#now()#" cfsqltype="cf_sql_date">)
      </cfquery>
</cfloop>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
whaleykAuthor Commented:
Thanks very much Rob.  Received the error:
Object of type class coldfusion.runtime.Struct cannot be used as an array

so changed to:
<cfloop from="1" to="#structCount(fileInfo.trackingNo)#" index="x">

Works :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.