Multiple database inserts

How to insert multiple uploads to a database

Page 1 of 1

3 Replies - 4183 Views - Last Post: 16 December 2008 - 10:06 AM Rate Topic: -----

#1 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 195
  • Joined: 03-December 08

Multiple database inserts

Post icon  Posted 15 December 2008 - 11:16 AM

I am trying to figure out how to insert multiple uploads to a database. In the following html code I have two input fields that are meant to accept two separate images. I have successfully uploaded both images to a folder, but I would also like to insert each to a database table.

The HTML:
<form method="post" name="uploader" action="sample_upload.cfm" enctype="multipart/form-data" onsubmit="return formCheck();">
<table width="550" border="1" align="center" cellpadding="0" cellspacing="1" bordercolor="#999999">
  <tr> 
	<td bgcolor="#EEEEEE"><div align="left"><font color="#fff" size="3" face="Arial, Helvetica, sans-serif"><strong>&nbsp;Browse to Find Images to Upload</strong></font></div></td>
  </tr>
  <tr> 
	<td>
<input type="file" name="uploada" value="" class="form">
<input type="file" name="uploadb" value="" class="form">
	</td>
  </tr>
  <tr>
	<td>
<input name="description" type="text" id="description" size="30" class="form">
		Please Provide a Short Description of Your Image</td>
  </tr>
  <tr>
	  <td>
<input name="Submit" type="submit" value="Upload New Image" class="submit">
	  </td>
  </tr>
</table>
</form>



The Colf Fusion action page:
<cfset uploads="C:\ColdFusion8\wwwroot\sample\gallery\">
	   
<cfif "form.uploada" NEQ ''>
<CFFILE ACTION="upload"				
FILEFIELD="uploada"
DESTINATION="#uploads#"
NAMECONFLICT="MAKEUNIQUE">				
<cfset file1= #file.serverfile#>
		  
</cfif>
		
<cfif "form.uploadb" NEQ ''>
<CFFILE ACTION="upload"
FILEFIELD="uploadb"
DESTINATION="#uploads#"
NAMECONFLICT="MAKEUNIQUE">
<cfset file2= #file.serverfile#>
			 
</cfif>	
			
<cfquery name="InsertDetails" datasource="sample">
	  INSERT INTO gallery(sm_img, lg_img) 
	  VALUES ('#file1#','#file1#') 
	  </cfquery>
	  
	  <cfquery name="InsertDetails" datasource="sample">
	  INSERT INTO gallery(sm_img, lg_img) 
	  VALUES ('#file2#','#file2#') 
	  </cfquery>



As you can see in the CF action page I have two cffile uploads and two cfquery inserts in order to accomplish the upload and insertion of two separate image files. But what if I select and upload a large number of images? Like 10 images? What is the process for cffile uploads, and database insertion for multiple files?

Thanks for any tips and tricks you may be able to provide!

Regards

Is This A Good Question/Topic? 0
  • +

Replies To: Multiple database inserts

#2 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Multiple database inserts

Posted 15 December 2008 - 03:44 PM

View Postmidasxl, on 15 Dec, 2008 - 10:16 AM, said:

As you can see in the CF action page I have two cffile uploads and two cfquery inserts in order to accomplish the upload and insertion of two separate image files. But what if I select and upload a large number of images? Like 10 images? What is the process for cffile uploads, and database insertion for multiple files?


You will need separate inserts regardless. But you can simplify the code using dynamic field names. One option is to name the file fields sequentially, using a loop. The resulting field names will be something like:

img_1, img_2, ... img_10.

The total number of fields is stored in a hidden field:

<!--- generate 10 file fields --->
<cfset maxFiles = 10>

<cfoutput>
  <cfloop from="1" to="#maxFiles#" index="counter">
	<input type="file" name="img_#counter#">
  </cfloop>

  <input type="hidden" name="numOfFiles" value="#maxFiles#">
</cfoutput>




Then use another loop on the action page and inside the loop a) upload each file and B) insert the file name into your database table. The loop uses the counter variable to construct the correct "filefield" names. Not tested and in desperate need of error handling, but here is the general idea:

<cfparam name="form.numOfFiles" default="0">

<!--- upload each file --->
<cfloop from="1" to="#form.numOfFiles#" index="counter">
		<cfoutput>DEBUG TEXT: Uploading file form.img_#counter#<br></cfoutput>

	<CFFILE ACTION="upload"
		FILEFIELD="img_#counter#"
		DESTINATION="#uploadDirectory#"
		NAMECONFLICT="MAKEUNIQUE">

	<cfquery name="InsertDetails" datasource="sample">
		  INSERT INTO yourTable(sm_img, lg_img)
		  VALUES ('#CFFILE.serverFile#', .... )
		  </cfquery>
		   

</cfloop>



BTW, #FILE# is deprecated. So you should use either the "result" attribute or #CFFILE#
http://livedocs.adob.../Tags_f_10.html
Was This Post Helpful? 0
  • +
  • -

#3 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 195
  • Joined: 03-December 08

Re: Multiple database inserts

Posted 16 December 2008 - 09:21 AM

Thanks for the info!!

Works great!

I have also been messing around with multiple uploads via flex and flash. Using something similar to the following...

http://www.e3webdesi...FileUpload.html

I've got this uploading all selected files into a directory, but is there a way to get the files inserted into a database at the same time? Or do I need to load them from the directory (after they have been uploaded into it) into the database?

I imagine this would have to be a loop based process since I will never know ahead of time how many images the end user will select for upload.

Thanks again for your time, I greatly appreciate it!!
Was This Post Helpful? 0
  • +
  • -

#4 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Multiple database inserts

Posted 16 December 2008 - 10:06 AM

View Postmidasxl, on 16 Dec, 2008 - 08:21 AM, said:

I've got this uploading all selected files into a directory, but is there a way to get the files inserted into a database at the same time? Or do I need to load them from the directory (after they have been uploaded into it) into the database?


That is what the example above should do. Though I am not sure what your flash/flex code looks like. Just put the insert query inside the same loop that does the upload. In other words:

1) upload the file first then
2) do the insert query
.. repeat for as many files as you have.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1