Question
Looping over an update query
Hello everyone. I am trying to update individual rows in my
db table by using cfloop. I am having trouble figuring out the
WHERE section of my update query. As it stands right now, I update
every row with the same values instead of stepping through each
row.
<!--- Update multiple rows --->
<cfset id = '#form.id#'>
<cfset counter = '#form.counter#'>
<cfset loop_value = '#form.loop_value#'>
<cfloop index="loop" from="1" to="#loop_value#" step="1">
<cfset name = form["name" & "#counter#"]>
<cfset title = form["title" & "#counter#"]>
<cfquery name="update" datasource="mydb">
UPDATE names
SET
group_officer_name = '#group_officer_name#',
group_officer_title = '#group_officer_title#'
WHERE
id = '8'
</cfquery>
<cfset counter = ('#counter#' - 1)>
</cfloop>
WHERE id = '8' needs to be the primary key for each row but I'm not sure how to do that since the values come from a dynamic form where the unique values are designated by the form value name with a #counter# attached to the end. If I do that for the primary key the row won't update at all.
<!--- Update multiple rows --->
<cfset id = '#form.id#'>
<cfset counter = '#form.counter#'>
<cfset loop_value = '#form.loop_value#'>
<cfloop index="loop" from="1" to="#loop_value#" step="1">
<cfset name = form["name" & "#counter#"]>
<cfset title = form["title" & "#counter#"]>
<cfquery name="update" datasource="mydb">
UPDATE names
SET
group_officer_name = '#group_officer_name#',
group_officer_title = '#group_officer_title#'
WHERE
id = '8'
</cfquery>
<cfset counter = ('#counter#' - 1)>
</cfloop>
WHERE id = '8' needs to be the primary key for each row but I'm not sure how to do that since the values come from a dynamic form where the unique values are designated by the form value name with a #counter# attached to the end. If I do that for the primary key the row won't update at all.