0

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>
SOS
  • 6,430
  • 2
  • 11
  • 29
user1824615
  • 49
  • 1
  • 5
  • Yes you need to put it into a loop of some sort. Also use `` to protect against SQL injection. – James A Mohler Sep 23 '19 at 03:30
  • 1
    To see what is happening, replace the cfquery tag with a cfoutput tag. Also, looping through a list without an index attribute looks suspicious. – Dan Bracuk Sep 23 '19 at 03:32
  • 2
    The SQL is incorrect to. Should be Values not value? – haxtbh Sep 23 '19 at 12:18
  • 2
    The description *chokes out at the insert portion* is very ambiguous. What actually happens? If you're getting an error, please [edit] your question to include the full error message. Also, review the documentation for [cffile action=uploadAll](https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/tags-f/cffile-action-uploadall.html). It returns an *array* of structures containing file information. You need to loop through that array, not the form field. – SOS Sep 23 '19 at 17:24

1 Answers1

0

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>
SOS
  • 6,430
  • 2
  • 11
  • 29
S J kali
  • 21
  • 3