Solved

Add a description name to the database

Posted on 2014-04-03
6
212 Views
Last Modified: 2014-04-07
Hi,

I have file upload text box and a text box for the description.

I am having a problem inserting File description to my database.

Is this correct -> value="#Descp[ii]#"

this is what I have

   <cfoutput>
                  <cfloop from="1" to="3" index="ii"> <tr> 
                  
                  <td style="width:120px;">File Description</td>
                    <td rowspan="2" align="left" valign="top" style="padding-right:5px;">
                      
                    <INPUT TYPE="input" NAME="FileDescription#ii#" id="FileDescription#ii#" style="width:400px;">
      <INPUT TYPE="FILE" NAME="AttachFile#ii#" id="AttachFile#ii#" onchange="return validateFileExtension(this);" class="file" style="width:400px;">
      <input type="button" value="Delete" name="DeleteFile#ii#" id="DeleteFile#ii#" style="height:20px;float:right;margin-right:87px;" onclick="clearFile('AttachFile' + #ii#,'FileDescription' + #ii# );" />

                 </tr>
                  <tr>
                    <td style="width:120px;">&nbsp;</td>
                  </tr>
                 </cfloop>
                 </cfoutput> 

Open in new window



<!--- Insert Uploaded files --->
 <cfset filelocation = expandpath('uploadedfiles/')> 
<cfset maxFiles = 3>
<cfset Attachments="">
      <cfloop from="1" to="#variables.maxFiles#" index="ii">
      <cftry>
            <cfif StructKeyExists(form,"AttachFile" & ii) && len(trim(Form["AttachFile" & ii]))>
             
             <cfset Descp[ii] = #Form["FileDescription" & ii]#> 

             <CFFILE ACTION="UPLOAD"
                        FILEFIELD="AttachFile#ii#"
                        DESTINATION="#variables.filelocation#"
                        NAMECONFLICT="makeunique">
     
             <cfset Attachments=ListAppend(variables.Attachments,cffile.serverfile)>  
          
         </cfif>
            <cfcatch>
            <br>#cfcatch.message#<br>#cfcatch.detail#<br>
            #cfcatch.tagContext[1].template# Line #cfcatch.tagContext[1].line#
            </cfcatch></cftry>
      </cfloop>

            <cfif len(trim(variables.Attachments))> 
   
      <cfquery name = "QLogFiles"  datasource="#strDSN#" username="#strUID#" password="#strPWD#">
			Set Nocount On;

                   <cfloop list="#variables.Attachments#" index="ii">
            
      <cfif FileExists(variables.filelocation & ii)>
      
                  INSERT INTO Files
                        (FileName,UploadedFile,IssueID,Status,CreatedDate)
                  VALUES
                        (
                        <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Descp[ii]#">,           
                        <cfqueryparam cfsqltype="cf_sql_blob"
                                value="#FileReadBinary(variables.filelocation & ii)#">,
                    #IssueIDVal#, 
                    1, 
                    <cfqueryparam cfsqltype="cf_sql_date" value="#Now()#" />  )          
        </cfif> 
      </cfloop>
</cfquery>
</cfif>

Open in new window

0
Comment
Question by:lulu50
6 Comments
 
LVL 15

Expert Comment

by:myselfrandhawa
Comment Utility
Your code is fine, no issues

Here is the screenshot, but there are far better ways to do the same thing you did. If i get some time by evening, i will post the better way of handling this

regards
upload.png
0
 
LVL 19

Expert Comment

by:erikTsomik
Comment Utility
also check the dataType in the database make sure the description field in the database allows that many characters that  you are trying to insert
0
 
LVL 24

Accepted Solution

by:
dgrafx earned 500 total points
Comment Utility
Hello
First off there isn't a "type=input" - it's "type=text" ...

So here would be the code:
<cfloop from="1" to="3" index="ii">
	<INPUT TYPE="text" NAME="FileDescription#ii#" id="FileDescription#ii#" style="width:400px;">
	<INPUT TYPE="FILE" NAME="AttachFile#ii#" id="AttachFile#ii#" onchange="return validateFileExtension(this);"
		class="file" style="width:400px;">
	<input type="button" value="Delete" name="DeleteFile#ii#" id="DeleteFile#ii#"
		style="height:20px;float:right;margin-right:87px;" 
		onclick="clearFile('AttachFile' + #ii#,'FileDescription' + #ii# );" />
	<br /><br />
</cfloop>

Open in new window



You had a couple mistakes - what you have wouldn't exactly work - would throw an error.
see the code I added below - Pay particular attention to:
<cfset variables["Descp" & ii] = Form["FileDescription" & ii]>

Open in new window

and then in the query code (note that you need to add the counter var named cnt for this)
<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["Descp" & cnt]#">

Open in new window


Note that I haven't tested so may have a couple issues - just post back if you do ...

<cfset filelocation = expandpath('uploadedfiles/')> 
<cfset maxFiles = 3>
<cfset Attachments="">
	<cfloop from="1" to="#variables.maxFiles#" index="ii">
      	<cftry>
            <cfif StructKeyExists(form,"AttachFile" & ii) && len(trim(Form["AttachFile" & ii]))>
             
				<cfset variables["Descp" & ii] = Form["FileDescription" & ii]>
             	<CFFILE ACTION="UPLOAD"
                        FILEFIELD="AttachFile#ii#"
                        DESTINATION="#variables.filelocation#"
                        NAMECONFLICT="makeunique">
     
             	<cfset Attachments=ListAppend(variables.Attachments,cffile.serverfile)>  
          
         	</cfif>
		<cfcatch>
			<br>#cfcatch.message#<br>#cfcatch.detail#<br>
			#cfcatch.tagContext[1].template# Line #cfcatch.tagContext[1].line#
		</cfcatch></cftry>
	</cfloop>
	<!--- ============================================ --->
	<cfif len(trim(variables.Attachments))> 
	
		<cfquery name = "QLogFiles"  datasource="#strDSN#" username="#strUID#" password="#strPWD#">
		Set Nocount On;
	
		<cfset cnt=0>
		<cfloop list="#variables.Attachments#" index="ii">
			<cfset cnt++/>	
			<cfif FileExists(variables.filelocation & ii)>
			
				INSERT INTO Files
				(FileName,UploadedFile,IssueID,Status,CreatedDate)
				VALUES
				(
				<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#variables["Descp" & cnt]#">,           
				<cfqueryparam cfsqltype="cf_sql_blob"
					value="#FileReadBinary(variables.filelocation & ii)#">,
				#IssueIDVal#, 
				1, 
				<cfqueryparam cfsqltype="cf_sql_date" value="#Now()#" />
				) 
				         
			</cfif>			
		</cfloop>
		
		</cfquery>
		
	</cfif>

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Expert Comment

by:myselfrandhawa
Comment Utility
@dgrafx.

His code will work fine, only thing is it is not a best practice, the code you showed is a way good and very neat and clean.
0
 
LVL 24

Expert Comment

by:dgrafx
Comment Utility
sorry to disagree but it absolutely is best practice!

good luck ...
0
 

Author Closing Comment

by:lulu50
Comment Utility
Thank you for your help!!!!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now