Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

File upload and data insert problem

Posted on 2011-02-18
6
Medium Priority
?
369 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:jasch2244
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 34928848
<cfoutput>#form.NamedFile#+#x#</cfoutput>


For dynamic field names, use array notation  ie

        #form["namedFiled"& x]#

instead of

       #form.NamedFile#+#x#

0
 
LVL 52

Expert Comment

by:_agx_
ID: 34928876
<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.




0
 
LVL 1

Author Comment

by:jasch2244
ID: 34929079
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

0
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
LVL 52

Expert Comment

by:_agx_
ID: 34929259
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..).
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 34929337
INSERT INTO files (FileName,ClosingID,NamedFile)

Oh wait, I see the problem.  The "NamedFile" column is a string.  So you either need to enclose the value in single quotes

           INSERT INTO files (NamedFile, ....etc)
           VALUES( '#form["NamedFile"& x]#', ... etc )

OR  use cfqueryparam

           VALUES(
               <cfqueryparam value="#form["NamedFile"& x]#" cfsqltype="cf_sql_varchar">
               , ... etc....
            )
0
 
LVL 1

Author Closing Comment

by:jasch2244
ID: 34929863
You are awesome! I don't know what I would do without _agx. Thanks again :)
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question