Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,690 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,508 people online right now. Registration is fast and FREE... Join Now!




update multiple rows of db from user form

 
Reply to this topicStart new topic

update multiple rows of db from user form

jeffmc21
post 14 Oct, 2008 - 11:57 AM
Post #1


New D.I.C Head

*
Joined: 27 Nov, 2007
Posts: 46


My Contributions


Hey guys..

I have a form that basically has seven rows of input. I would like to be able to take the user-inputted data from the form and update the database. In effect, each week I'd want to empty the database's contents (which I do with a DELETE FROM statement at the top of my response page) and then re-populate it with the info submitted from the form.

I thought that using some type of cfloop over a query would work, but I'm very inexperienced with CF, so it's proving not so easy. Is that the best way? and if so, how would I need to organize the cfloop statement? I'm getting conflicting examples from the web.

Thanks for your help in advance.
User is offlineProfile CardPM

Go to the top of the page


sansclue
post 14 Oct, 2008 - 04:03 PM
Post #2


D.I.C Head

**
Joined: 21 Nov, 2007
Posts: 104



Thanked 6 times
My Contributions


IMO the easiest way is to name the form fields dynamically using a counter. So each set of fields will be numbered from 1 to N:

CODE

    form.firstName1, form.lastName1
    form.firstName2, form.lastName2
    ...
    form.firstNameN, form.lastNameN



Cfquery's currentRow variable makes an good counter:

CODE

<form ...>
  <cfoutput query="yourQuery">
    <input type="text" name="firstName#currentRow#" value="#firstName#">
    <input type="text" name="lastName#currentRow#" value="#lastName#">
     ....
   </cfoutput>
    
</form>


Outside the query, save the total number of records in a hidden form field. You can obtain the total from cfquery's recordCount variable.


Then on your processing page, use a from/to loop to extract each set of values. Assuming your form uses method="post", the fields will be stored in the FORM structure. To retrieve the values use array notation:

#form["yourFieldName"& counterNumber]#

CODE

<cfloop from="1" to="#form.totalNumberOfRecords#" index="counter">
     <!--- extract the values of the dynamic form fields --->
     <cfset variables.firstName = form["firstName"& counter]>
     <cfset variables.lastName = form["lastName"& counter]>

     .... your query goes here ...
  
</cfloop>


Once you have extracted the values, the rest is just constructing your INSERT or UPDATE query.
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 08:13AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month