?
Solved

Add a description name to the database

Posted on 2014-04-03
6
Medium Priority
?
222 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
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 
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

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

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, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

771 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