Solved

Add a description name to the database

Posted on 2014-04-03
6
218 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 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39977465
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
ID: 39977935
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 25

Accepted Solution

by:
dgrafx earned 500 total points
ID: 39978023
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
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 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39978329
@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 25

Expert Comment

by:dgrafx
ID: 39978362
sorry to disagree but it absolutely is best practice!

good luck ...
0
 

Author Closing Comment

by:lulu50
ID: 39982954
Thank you for your help!!!!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 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

13 Experts available now in Live!

Get 1:1 Help Now