cffileupload and inserting a record to a database
I'm really hoping someone can help me with this as I've spent hours trawling through all the currently available information and haven't been able to get this to work. I want to include the new cffileupload tool on the property site I'm working on but I need to be able to upload the photos and enter a new record into the database connecting those photos with the property they relate to so that they will then be served up on the property details page. I have a version that works using cffile upload but not the new interface. I'm also quite new to ColdFusion, so learning all the time. Here's the code that doesn't work:
FORM FILE:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en-UK">
<head>
</head>
<body>
<cfform action="cffileupload_action.cfm">
<!--- Choose Property to Upload Photos for --->
<div id="chooseproperty">
<fieldset id="chooseproperty">
<legend><span>Choose Property</span></legend>
<cfselect name="propertyname" query="propertylist" display="propertyname" queryposition="below"></cfselect><br />
</fieldset>
</div>
</cfform>
<!--- Choose Photos to Upload --->
<div id="photoupload">
<fieldset id="photoupload">
<legend><span>Upload Photos</span></legend>
<cffileupload url="cffileupload_action.cfm?#urlEncodedFormat(session.urltoken)#" extensionfilter=".jpg, .png, .jpeg" maxuploadsize="1" title="Photo Uploader" width="600" addbuttonlabel="Select Photos" clearbuttonlabel="Remove All Photos" deletebuttonlabel="Remove Photo" />
<input type="submit" value="Submit" id="controls">
</fieldset>
</div>
</div><!-- end of content div -->
</body>
</html>
ACTION FILE:
<cfquery name="lookupID" datasource="BKtest">
SELECT propID
FROM properties
WHERE propertyname = "#Form.propertyname#"
</cfquery>
<cffile action="uploadall" destination="/Applications/MAMP/htdocs/beckleykenny/images/properties" nameConflict="Overwrite" />
<cfquery name="addfilename" dataSource="BKtest">
INSERT INTO images(imagefilename, propID)
VALUES('#CFFILE.SERVERFILE#', '<cfoutput query="lookupID">#propID#</cfoutput>')
</cfquery>
HOWEVER, this works so it's obviously something to do with the presence of the query & the output of that query. What happens is that the upload won't work. This works (I've just substituted the query output value for the actual real value of the ID in the database for testing purposes):
<cffile action="uploadall" destination="/Applications/MAMP/htdocs/beckleykenny/images/properties" nameConflict="Overwrite" />
<cfquery name="addfilename" dataSource="BKtest">
INSERT INTO images(imagefilename, propID)
VALUES('#CFFILE.SERVERFILE#', '2')
</cfquery>
In order for it to be reasonably foolproof, it has to be a <cfselect> so that the propertyname can be correctly chosen from the database, though I couldn't get it to work when I tried making it an ordinary <form> and substituting an <input> field so that the propertyname could be typed in. Submitting that form simply resulted in the file being uploaded but when the form was submitted, an error was generated saying that there wasn't any file uploaded to associate with the form data!
I sincerely hope someone can help me work out why it's not working. I know it's something to do with having the query in the action page but I can't see how I'm going to fetch the id of the property any other way.
