1 Replies - 2385 Views - Last Post: 19 January 2010 - 01:33 PM Rate Topic: -----

#1 dean2288   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-January 10

multi file upload and insert

Posted 15 January 2010 - 11:41 PM

I need a script that will allow up to 4 images to be uploaded and the names inserted into a DB. I am using coldfusion and MSSQL. I have a working version that will upload 1 - 4 images, but I cannot seem to get an insert query that puts all the names in correctly. I get all four feilds with image 1's name and thats it? Does anyone know how to write an insert statement that will insert the name of each image and if they upload less than the 4 images, I want to insert "default.jpg" in the empty feilds. Any help would be appreciated. Thanks in advance. Here is the code I am using:

cfset numberoffields = 4>
<cfif isdefined("form.upload")>
<cfset message = "">
<cfloop index="i" from="1" to="#variables.numberoffields#" step="1">
<cfset filename = "form.file" & #i#>
<cfif evaluate(variables.filename) neq "">
<cffile action="UPLOAD"
<cfset message = message & ",File%20#i#%20(#file.serverfile#)%20was%20uploaded">

<cfset message = message & ",File%20#i#%20was%20empty">
<cflocation url="FileUpload.cfm?owner=<cfoutput>#url.owner#</cfoutput>&msg=#variables.message#">
<h2>File Upload</h2><cfif isdefined("url.msg")>
<div style="color:#FF0000;">
<cfloop list="#url.msg#" index="i">
<cfoutput>#i#</cfoutput><br />
<form action="FileUpload.cfm?owner=<cfoutput>#url.owner#</cfoutput>" enctype="multipart/form-data" method="post">
<cfloop index="i" from="1" to="#variables.numberoffields#" step="1">
<cfset filename = "file" & #i#>
<input type="File" name="<cfoutput>#variables.filename#</cfoutput>" /><br />
<input name="Item_Owner_ID" type="hidden" value="<cfoutput>#url.owner#</cfoutput>">
<input type="Submit" name="upload" value="upload">

Is This A Good Question/Topic? 0
  • +

Replies To: multi file upload and insert

#2 Craig328   User is offline

  • I make this look good
  • member icon

Reputation: 2024
  • View blog
  • Posts: 3,609
  • Joined: 13-January 08

Re: multi file upload and insert

Posted 19 January 2010 - 01:33 PM


There isn't any code in the code example you posted that even tries to run an insert query. That said, if you're wanting to amend the code sample you provided, you'd probably be best served by doing it in the same cfloop sequence you use to upload the image with the cffile tag.
  • Start the loop over the file input fields
  • Start a cftry/cfcatch block
  • Upload the file using cffile
  • If successful and no errors, then do an insert into the appropriate table in your database
  • End the cftry/cfcatch block

What the try/catch does is ensure that you'll only be adding entries to your database for successful file uploads.

Good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1