Link to home
Start Free TrialLog in
Avatar of jasch2244
jasch2244

asked on

File upload and data insert problem

I'm having an error of: Element NAMEDFILE is undefined in FORM in my code attached. I had someone help me with this code on this site months back and it works great. I have added a new input tag that allows me to name or categorize files that are being uploaded. Looks like I can't set the correct variable name in the loop as it is sent to the "action page".

Can someone help me with this please?
/////FORM//////
<form action="File_Upload_Action.cfm" method="post" enctype="multipart/form-data">
<table width="500" border="0" cellspacing="2" cellpadding="0">
	<cfloop from="1" to="#numberOfFiles#" index="x">
  <tr>
    <td width="33">#x#</td>
    <td width="208">Name 
      <input name="NamedFile#x#" type="text" id="NamedFile" /></td>
    <td width="174"><input name="uploadFile#x#" type="file" size="10"></td>
    <td width="71">&nbsp;</td>
  </tr>
   </cfloop>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input name="uploadButton" type="submit" value="Upload Files">
    <input type="hidden" name="numberOfFiles" value="#numberOfFiles#">
    <input type="hidden" name="ClosingID" value="#ClosingID#">    </td>
    <td>&nbsp;</td>
    <td width="2">&nbsp;</td>
    </tr>
</table>
</form>

/////ACTION PAGE/////
<cfif structKeyExists(form, "uploadButton")>
	<cfparam name="form.numberOfFiles" default="0">
	<!--- replace this with your folder path --->
	<cfset folderToStoreFiles = ExpandPath("Files")>
	<!--- create the directory if it does not exist --->
	<cfif not DirectoryExists(folderToStoreFiles)>
		<cfdirectory action="create" directory="#folderToStoreFiles#">
	</cfif>
	
	<!--- process each file ...--->
	<cfloop from="1" to="#form.numberOfFiles#" index="x">
		<!--- if a file was supplied, upload it ... --->
		<cfif structKeyExists(form, "uploadFile"& x) AND len(trim(form["uploadFile"& x]))>
			<!--- upload the current file to the desired directory --->
	 		<cffile action="upload"
				filefield="uploadFile#x#" 
    			destination="#folderToStoreFiles#" 
                accept = "application/pdf,image/jpeg"
    			nameconflict="makeunique">
	
    		<!--- insert the file name into your database --->
			<cfquery name="insertFile" datasource="#application.datasource#">
				INSERT INTO files (FileName,ClosingID,NamedFile)
				VALUES 
				(
					<cfqueryparam value="#cffile.serverFile#" cfsqltype="cf_sql_varchar">,
                    <cfoutput>#form.ClosingID#</cfoutput>,<cfoutput>#form.NamedFile#+#x#</cfoutput>
				)
			</cfquery>
 
    		<!--- debugging: show uploaded file info --->
			
				<img src="../images/CheckIcon.png" align="absmiddle" /> <cfoutput>#cffile.serverFile#</cfoutput> is uploaded!<br>
			
		<cfelse>		
	    	<!--- debugging: show error --->
			<cfoutput>
				<img src="../images/None.png"  align="absmiddle" />  Processing file #x#: No file found. Skipping ...<br>
		  </cfoutput>
	  </cfif>
	</cfloop>
</cfif>

Open in new window

Avatar of _agx_
_agx_
Flag of United States of America image

<cfoutput>#form.NamedFile#+#x#</cfoutput>


For dynamic field names, use array notation  ie

        #form["namedFiled"& x]#

instead of

       #form.NamedFile#+#x#

<cfquery name="insertFile" datasource="#application.datasource#">
INSERT INTO files (FileName,ClosingID,NamedFile)
VALUES
(
     <cfqueryparam value="#cffile.serverFile#" cfsqltype="cf_sql_varchar">,
    <cfoutput>#form.ClosingID#</cfoutput>,<cfoutput>#form.NamedFile#+#x#</cfoutput>
)
</cfquery>

Not having to do with your error but
1) You don't need to use <cfoutput> within a <cfquery>, or most CF tags for that matter. The #variables# will be evaluated automatically.
2) you should use cfqueryparam on *all* values, not just some of them.




Avatar of jasch2244
jasch2244

ASKER

ok I think I updated the code and now am getting a: Unknown column 'tES' in 'field list'  tES is what I typed in for the file name in the NamedFile input... I thought it is looping through the NamedFile#x# and would insert to the column  NamedFile in the files table. My updated code is attached.

Regarding the cfqueryparam I just don't ever know when to use it or not... it's just for query's with updates and inserts no?
////FORM PAGE///

<form action="File_Upload_Action.cfm" method="post" enctype="multipart/form-data">
<table width="500" border="0" cellspacing="2" cellpadding="0">
	<cfloop from="1" to="#numberOfFiles#" index="x">
  <tr>
    <td width="33">#x#</td>
    <td width="208">Name 
      <input name="NamedFile#x#" type="text" id="NamedFile" /></td>
    <td width="174"><input name="uploadFile#x#" type="file" size="10"></td>
    <td width="71">&nbsp;</td>
  </tr>
   </cfloop>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input name="uploadButton" type="submit" value="Upload Files">
    <input type="hidden" name="numberOfFiles" value="#numberOfFiles#">
    <input type="hidden" name="ClosingID" value="#ClosingID#">    </td>
    <td>&nbsp;</td>
    <td width="2">&nbsp;</td>
    </tr>
</table>
</form>

////ACTION PAGE///
<cfif structKeyExists(form, "uploadButton")>
	<cfparam name="form.numberOfFiles" default="0">
	<!--- replace this with your folder path --->
	<cfset folderToStoreFiles = ExpandPath("Files")>
	<!--- create the directory if it does not exist --->
	<cfif not DirectoryExists(folderToStoreFiles)>
		<cfdirectory action="create" directory="#folderToStoreFiles#">
	</cfif>
	
	<!--- process each file ...--->
	<cfloop from="1" to="#form.numberOfFiles#" index="x">
		<!--- if a file was supplied, upload it ... --->
		<cfif structKeyExists(form, "uploadFile"& x) AND len(trim(form["uploadFile"& x]))>
			<!--- upload the current file to the desired directory --->
	 		<cffile action="upload"
				filefield="uploadFile#x#" 
    			destination="#folderToStoreFiles#" 
                accept = "application/pdf,image/jpeg"
    			nameconflict="makeunique">
	
    		<!--- insert the file name into your database --->
			<cfquery name="insertFile" datasource="#application.datasource#">
				INSERT INTO files (FileName,ClosingID,NamedFile)
				VALUES 
				(
					<cfqueryparam value="#cffile.serverFile#" cfsqltype="cf_sql_varchar">,
                    #form.ClosingID#,
                    #form["NamedFile"& x]#
				)
			</cfquery>
 
    		<!--- debugging: show uploaded file info --->
			
				<img src="../images/CheckIcon.png" align="absmiddle" /> <cfoutput>#cffile.serverFile#</cfoutput> is uploaded!<br>
			
		<cfelse>		
	    	<!--- debugging: show error --->
			<cfoutput>
				<img src="../images/None.png"  align="absmiddle" />  Processing file #x#: No file found. Skipping ...<br>
		  </cfoutput>
	  </cfif>
	</cfloop>
</cfif>

Open in new window

Can you post the full error message?  A quick search of the code doesn't show anything containing "tES".

I just don't ever know when to use it or not... it's just for query's with updates and inserts no?

Honestly, it should be used in all queries (insert, select, update, delete).  But especially when you're passing a user supplied value (FORM, URL, etc..).
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are awesome! I don't know what I would do without _agx. Thanks again :)