?
Solved

Add a description name to the database

Posted on 2014-04-03
6
Medium Priority
?
223 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
[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
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 2000 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
Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

650 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