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
Solved

File upload and data insert problem

Posted on 2011-02-18
6
363 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 500 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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