Solved

Add a description name to the database

Posted on 2014-04-03
6
219 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
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 …

726 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