Uploading the file names of my images to my database table

295 views Asked by At

I'm having an issue with uploading or looping through the the file names of my images to my database table.

I am able to select various images and upload the selected image on my local machine to a specific folder that I designate. After the image files are put into my folder, I am having some difficulties because I want to insert the file names of the images that I just inserted into my folder, into my database. I'm trying to use the cffile parameters to do that.

My question: Being that it is multiple files being uploaded at once, do I need to do a cfloop on a cfquery insert in order to capture and insert the names into my database? My attempt at the code is below. The upload workd just fine, but it chokes out at the insert portion. What do you think could be the issue?

<cfparam name="form.fileUpload3" default="">
<cfif len(trim(form.fileUpload3))>
   <cffile action="uploadall" 
      fileField="fileUpload3" 
      destination="#cookie.c.webpathmultipleimages#" 
      accept = "image/jpeg, image/png, image/jpeg" 
      nameconflict="makeunique">

   <!---This is where i'm running into my issue--->
   <cfloop INDEX="i" LIST="#Form.fileUpload3#">
      <cfquery name="addtoimage">
         insert into image (imaid, imacomid, imaname) 
         Value (
           '00',
          '#cookie.communityID#',
          '#file.clientfile#'
         )
      </cfquery>
</cfif>
1

There are 1 answers

2
S J kali On

The cffile returns an array, but you used a list loop. So, you got the error. Try it with an array loop.

<cfif len( form.image )>
    <cffile action="uploadall" filefield="image" 
        destination="#ExpandPath('.\uploadImages\')#" 
        result="fileName" 
        nameconflict="makeunique">
    <cfloop array="#fileName#" index="image">
        <cfquery name="imageUpload" datasource="uploadMultipleImage">
            insert into uploadImage (imageName) 
            values 
            ( 
               <cfqueryparam value="#image.serverfile#" cfsqltype="cf_sql_varchar">
            )
        </cfquery>
    </cfloop>
</cfif>