I have a table called categories:
CREATE TABLE IF NOT EXISTS `categories` (
`cat_id` smallint(2) NOT NULL auto_increment,
`category_id` smallint(2) NOT NULL,
`language_abbr` char(3) NOT NULL,
`category_name` varchar(32) NOT NULL,
`category_intro` text,
PRIMARY KEY (`cat_id`),
KEY `category_id` (`category_id`),
KEY `language_abbr` (`language_abbr`),
KEY `category_name` (`category_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
For the update funtionality I use a form from which the form fields are generated dynamically because the number of fields are depending on how many languages are active on the website.
<cfform method="post" preservedata="true">
<cfset rowCount = 0>
<cfloop index="i" from="1" to="#Ceiling( getlanguages.recordCount )#" step="1">
<cfset rowCount = rowCount + 1 >
<div class="form_wrapper">
<cfinput type="text" name="cat_id" value="#getCategory.cat_id[i]#">
<label class="label medium_label">Category #getlanguages.language_abbr[i]# <span>*</span></label>
</div>
<div class="form_wrapper">
<div class="input_bg input_bg_medium">
<cfinput type="text" name="category_name_#rowCount#" class="input input_medium" value="#getCategory.category_name[i]#">
</div>
</div>
<div class="form_wrapper">
<label class="label medium_label">Category Intro #getlanguages.language_abbr[i]#</label>
</div>
<div class="form_wrapper">
<div class="text_bg">
<cftextarea richtext="true" toolbar="Basic" skin="silver" name="category_intro_#rowCount#" width="520" height="200" >#getCategory.category_intro[i]#</cftextarea>
</div>
</div>
</cfloop>
</cfif>
<div class="form_wrapper">
<cfinput type="submit" name="submit_button" value="Submit" class="submit_button">
<cfinput type="hidden" name="counter" value="#Ceiling( getlanguages.recordCount )#">
</div>
</cfform>
By doing this I not only get the right number of form fields the category_names and intros are dynamically generated as well, which i need for the update query.
On the update page I have the following query:
<cfset listCount = 0 />
<cfloop index="fileField" from="1" to="#form.counter#" step="1">
<cfset listCount = listCount + 1 />
<cfset category_name = "Form.category_name_#listCount#">
<cfset category_name = Evaluate( category_name )>
<cfset category_intro = "Form.category_intro_#listCount#">
<cfset category_intro = Evaluate( category_intro )>
<cfquery datasource="#Application.dsn#">
UPDATE
categories
SET
category_name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim( category_name )#" >
, category_intro = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim( category_intro )#" >
WHERE
cat_id IN ( <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim( FORM.cat_id )#" list="true" /> )
</cfquery>
</cfloop>
and although this approach working fine when inserting new categories(obviously without the where clause) it doesn’t hold up in updating the right records. Instead of updating the appropriate records, it set the values of all records to the last one in the form loop?
Does anybody see what i’m doing wrong?