Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating Multiple Rows with CFINSERT

Status
Not open for further replies.

tempoman

Programmer
May 9, 2000
41
AU
Can someone please tell me how to update more than one row in the database with one submit button click.<br>I have a page that allows the user to select which interests they like and each of these interests have to be individual rows.<br><br>Thanx for your help
 
TEMPLATE ONE:

<!--- Get many rows of data --->
<cfquery name=&quot;qryCustomer&quot; datasource=&quot;aquatrac&quot;>
Select *
From tblCustomer
</cfquery>



<!--- Start a form out side a loop --->

<form action=&quot;rma.cfm&quot; method=&quot;post&quot;>
<!--- Set a hidden variable with the amount of records shown --->
<cfoutput><input type=&quot;Hidden&quot; name=&quot;RecordCount&quot; value=&quot;#qryCustomer.RecordCount#&quot;></cfoutput>

<!--- Start output INSIDE form tags. Lets say we have three form fields: ID, Desc, Status.
We name the form fields based off of what row they are in. If we have three rows, our fields would be:
id1, Desc1, Status1
id2, Desc2, Status2
id3, Desc3, Status3
--->
<cfoutput query=&quot;qryCustomer&quot;>
<input type=&quot;Hidden&quot; name=&quot;id#CurrentRow#&quot; value=&quot;#id#&quot;>
<input type=&quot;text&quot; name=&quot;Desc#CurrentRow#&quot; value=&quot;#Desc#&quot;>
<input type=&quot;text&quot; name=&quot;Status#CurrentRow#&quot; value=&quot;#Status#&quot;>

</cfoutput>

<!--- Have a single submit button outside the loop --->
<input type=&quot;Submit&quot; value=&quot;Save All&quot;>
</form>


TEMPLATE TWO (Insert or Update Records):


<!--- Run loop: one for every row outputed earlier --->

<cfloop index=&quot;i&quot; from=&quot;1&quot; to=&quot;#form.RecordCount#&quot;>


<!---



Now all we need to do is create variables within the loop that is composed of a prefix (&quot;form.formfield&quot;) and a suffix (i).
It is important to note that the prefix is a static string value (Notice in the code it is wrapped in quotes. This means that the
variable is assigned a literal value of form.whatever. The suffix however is a dynamic value. The value will be equal to the loop number.
So the first time that this loop is fired, the locID variable will be given the value of form.ID1. The possible confusing part at this point is
that locID is simply equal to the string form.ID1. But that string value happens to be the name of the form field that we are trying to evaluate.
So all we have to do at this point is use the evaluate function to give us the value of the form field form.ID1.
The main 'trick' in this script is assigning a literal string value that is the same as the form field that you want to evaluate.
--->




<cfset locID = &quot;form.ID&quot; & i>
<cfset locDesc = &quot;form.Desc&quot; & i>
<cfset locStatus = &quot;form.Status&quot; & i>

<!--- Note: All dynamic form variables are here evaluated. --->

<cfquery name=&quot;qryAddList#i#&quot; datasource=&quot;aquatrac&quot;>
INSERT INTO tblList
(ID, Desc, Status)
VALUES
('#Evaluate(locID)#', '#Evaluate(locDesc)#', '#Evaluate(locStatus)#')
</cfquery>








<!--- Loop to next row --->
</cfloop>
 
I am have a similar problem. I created a page that pulls up a list of records inside a form, with a text box that is filled with a number from the AbOrder field (used to rank results).

The idea is that the user can use this form to enter new numbers in the rank text box, which should update the individual records, allowing query results to have a new rank order.

I used your method above, but all it does is create NEW records, with only the two fields I am using in the form being filled, one of which is the StoryID field, which is &quot;AutoNumber&quot; and the primary key, and the AbOrder field.

Here is the code on the action page:

<cfloop index=&quot;i&quot; from=&quot;1&quot; to=&quot;#form.RecordCount#&quot;>
<cfset locStoryID = &quot;form.StoryID&quot; &amp; i>
<cfset locAbOrder = &quot;form.AbOrder&quot; &amp; i>
<cfquery name=&quot;Update_Abs#i#&quot; datasource=&quot;MyDb&quot;>
INSERT INTO Stories
(StoryID, AbOrder)
VALUES
('#Evaluate(locStoryID)#', '#Evaluate(locAbOrder)#')
</cfquery>
</cfloop>

I want only to UPDATE the records that are returned inside the form. I tried changing the query on the action page to an UPDATE, but I get an error.

The query looks like this:

<cfloop index=&quot;i&quot; from=&quot;1&quot; to=&quot;#form.RecordCount#&quot;>
<cfset locStoryID = &quot;form.StoryID&quot; &amp; i>
<cfset locAbOrder = &quot;form.AbOrder&quot; &amp; i>
<cfquery name=&quot;Update_Abs#i#&quot; datasource=&quot;MyDb&quot;>
UPDATE Stories
(StoryID, AbOrder)
VALUES
('#Evaluate(locStoryID)#', '#Evaluate(locAbOrder)#')
</cfquery>
</cfloop>

And the error looks like this:

ODBC Error Code = 37000 (Syntax error or access violation)

[Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement.

The error occurred while processing an element with a general identifier of (CFQUERY), occupying document position (5:1) to (5:66).

The line position numbers refer to the query name, datasource line.

Look forward you your answer.

Mo
 
GOT IT! Here's the query for the action page:

<cfloop index=&quot;i&quot; from=&quot;1&quot; to=&quot;#form.RecordCount#&quot;>
<cfset locStoryID = &quot;form.StoryID&quot; &amp; i>
<cfset locAbOrder = &quot;form.AbOrder&quot; &amp; i>
<cfquery name=&quot;Update_Abs#i#&quot; datasource=&quot;MyDb&quot;>
UPDATE Stories
SET AbOrder = #Evaluate(locAbOrder)#
WHERE StoryID = #Evaluate(locStoryID)#
</cfquery>
</cfloop>


Yippee,
Mo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top