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"
destination="c:\inetpub\webroot\myzippi\productimages\"
nameconflict="MAKEUNIQUE"
filefield="#variables.filename#">
<cfset message = message & ",File%20#i#%20(#file.serverfile#)%20was%20uploaded">
<cfelse>
<cfset message = message & ",File%20#i#%20was%20empty">
</cfif>
</cfloop>
<cflocation url="FileUpload.cfm?owner=<cfoutput>#url.owner#</cfoutput>&msg=#variables.message#">
<cfelse>
<h2>File Upload</h2><cfif isdefined("url.msg")>
<div style="color:#FF0000;">
<cfloop list="#url.msg#" index="i">
<cfoutput>#i#</cfoutput><br />
</cfloop>
</div>
</cfif>
<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 />
</cfloop>
<input name="Item_Owner_ID" type="hidden" value="<cfoutput>#url.owner#</cfoutput>">
<input type="Submit" name="upload" value="upload">
</form>
multi file upload and insert
Page 1 of 11 Replies - 2371 Views - Last Post: 19 January 2010 - 01:33 PM
Replies To: multi file upload and insert
#2
Re: multi file upload and insert
Posted 19 January 2010 - 01:33 PM
Dean:
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.
What the try/catch does is ensure that you'll only be adding entries to your database for successful file uploads.
Good luck!
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!
Page 1 of 1