4 Replies - 3208 Views - Last Post: 30 August 2010 - 11:27 AM Rate Topic: -----

#1 mpryan73  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 05-February 10

Update Multiple Records from Dynamic Form

Posted 26 August 2010 - 07:36 PM

I have a database with Images and Descritions of those images. Each image record has a unique ID

ID, Image, Description

I've created a form dynamicly with the Image ID, Image and Description.


<cfform name="UpdatePictures" method="post">
<table>
<cfoutput query="EditPictures">

	<tr>
		<td>
                    <input type="hidden" name="ID" value="#EditPictures.PhotoID#">

		  <img src="#EditPictures.image#">
		   
		</a>
		</td>
		<td>
                   
		<input type="text" name="D_#EditPictures.PhotoID#" value="#EditPictures.Description#" class="CaptionInput" ondblclick="this.value=''"/>
		</td>
	</tr>
</cfoutput>
</table>
<cfinput type="submit" name="Submit" value="Update Captions">
</cfform>




I'm trying to make it where you can update multiple Descriptions at once. I know you have to loop trough somehow but I can't put my finger on it.



Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Update Multiple Records from Dynamic Form

#2 xheartonfire43x  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: Update Multiple Records from Dynamic Form

Posted 27 August 2010 - 12:44 PM

Looking at what you are doing I see you are building a dynamic form with multiple images and image description fields. I am guessing it is something like Facebook's image post upload form.
You will have to first (to make things nice and easy) build a list of all the image id's that will be updated. Your form would be built something like this:
<cfsilent>
     <cfset imageids = chr(0) />
</cfsilent>
<cfform name="UpdatePictures" method="post">
<table>
<cfoutput query="EditPictures">
	<cfset imageids = imageids & ',' & EditPictures.PhotoID />
	<cfinput type="hidden" name="DO_#EditPictures.PhotoID#" value="#EditPictures.Description#" />
	<tr>
		<td>
			<cfinput type="hidden" name="ID" value="#EditPictures.PhotoID#" />
			<img src="#EditPictures.image#" />
		</td>
		<td>
			<cfinput type="text" name="D_#EditPictures.PhotoID#" value="#EditPictures.Description#" class="CaptionInput" ondblclick="this.value=''"/>
		</td>
	</tr>
</cfoutput>
</table>
<cfinput type="hidden" name="imageids" value="#imageids#" />
<input type="submit" name="Submit" value="Update Captions">
</cfform>



I added a few things in there to better your cf performance slightly
1) If your input doesn't have any dynamic data in it (anything with hash tags) it doesn't need to be a cfinput. Setting it as cfinput will cause Coldfusion to have to render that tag when it doesn't need to.
2) I added a new input called DO_#EditPictures.PhotoID# which will contain the original image description. Since we will be running multiple SQL Queries we don't want to have to run unnecessary ones. What we will do is compare that value to the value of D_#EditPictures.PhotoID# and if they are the same we know we don't have to update the field.

So the event handler is as follows:
<cfloop list="imageids" index="lIndex">
	<cfif FORM.DO_#lIndex# NEQ FORM.D_#lIndex#>
		<cfset currentDescription = FORM.D_#lIndex# />
		<cfquery datasource="{yourdatasourcename}">
			UPDATE tablename
			SET description = <cfqueryparam value="#currentDescription#" cfsqltype="cf_sql_longvarchar" />
			WHERE imageid = <cfqueryparam value="#lIndex#">
		</cfquery>
	</cfif>
</cfloop>



Not entirely sure if that will work. If it errors let me know what the error is.

And now onto the event handler.

This post has been edited by xheartonfire43x: 27 August 2010 - 12:45 PM

Was This Post Helpful? 1
  • +
  • -

#3 mpryan73  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 05-February 10

Re: Update Multiple Records from Dynamic Form

Posted 28 August 2010 - 03:42 PM

Thank you,

I saw a way somehow a long time ago but couldn't find it. I'm get an error though. at
<cfif FORM.DO_#lIndex# NEQ FORM.D_#lIndex# >


Invalid CFML construct found on line 7 at column 29.
ColdFusion was looking at the following text:
#

The CFML compiler was processing:
Was This Post Helpful? 0
  • +
  • -

#4 mpryan73  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 05-February 10

Re: Update Multiple Records from Dynamic Form

Posted 28 August 2010 - 05:50 PM

Got it!

Changed this

 <cfloop list="imageids" index="lIndex">  

02     <cfif FORM.DO_#lIndex# NEQ FORM.D_#lIndex#>  







<cfloop list="#imageids#" delimiters="," index="lIndex">

	<cfif #Evaluate("FORM.DO_#lIndex#")# NEQ #Evaluate("FORM.D_#lIndex#")# >
		<cfset currentDescription = #Evaluate("FORM.D_#lIndex#")# />



Works Great thanks
Was This Post Helpful? 0
  • +
  • -

#5 xheartonfire43x  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: Update Multiple Records from Dynamic Form

Posted 30 August 2010 - 11:27 AM

Good to hear. I didn't test anything, so to get that much with only 1 error I am quite proud of myself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1