我有一个表单,允许用户从整个集合中选择 4 条记录以特定顺序(1 到 4)出现。这些字段动态命名为 blah1、blah2 等。但是,更新查询不起作用,当我输出结果时,它们都默认为 1。
<cfquery name="allRecipes" datasource="#request.db#">
SELECT id, name, homepage_order
FROM tblrecipes
ORDER BY name
</cfquery>
<cfquery name="getOnlySelected" dbtype="query">
SELECT *
FROM allRecipes
WHERE homepage_order > 0
ORDER BY homepage_order
</cfquery>
<cfloop from="1" to="4" index="i">
<li>
Position <cfoutput>#i#</cfoutput>: Current recipe published: <strong><cfoutput>#getOnlySelected['name']["#i#"]#</cfoutput></strong><br />
Choose New:
<cfselect name="position#i#" query="allRecipes" value="id" display="name" queryPosition="below" selected="#getOnlySelected['id']['#i#']#">
<option value="">-Select-</option>
</cfselect>
</li>
</cfloop>
和表单提交查询:
<cfquery datasource="#request.db#">
UPDATE tblrecipes
SET homepage_order = 0
WHERE 1=1
</cfquery> //This query resets all the order values
<cfif IsNumeric(form.position1)>
<cfquery name="updateOrder1" datasource="#request.db#">
UPDATE tblrecipes
SET homepage_order = 1
WHERE id = #form.position1#
</cfquery>
</cfif>
<cfif IsNumeric(form.position2)>
<cfquery name="updateOrder2" datasource="#request.db#">
UPDATE tblrecipes
SET homepage_order = 2
WHERE id = #form.position2#
</cfquery>
</cfif>
<cfif IsNumeric(form.position3)>
<cfquery name="updateOrder3" datasource="#request.db#">
UPDATE tblrecipes
SET homepage_order = 3
WHERE id = #form.position3#
</cfquery>
</cfif>
<cfif IsNumeric(form.position4)>
<cfquery name="updateOrder4" datasource="#request.db#">
UPDATE tblrecipes
SET homepage_order = 4
WHERE id = #form.position4#
</cfquery>
</cfif>
这是表单数据的示例转储。每个位置的值都是正确的。
FIELDNAMES POSITION1,POSITION2,POSITION3,POSITION4,SUBMIT
POSITION1 81
POSITION2 82
POSITION3 80
POSITION4 78
SUBMIT Update Order
查询结果看起来一样,在表单提交之前和之后
HOMEPAGE_ORDER ID NAME
1 81 Okonomiyaki
1 82 Apple Chutney Cubes
1 80 Asparagus for the Family
1 78 Coconut Curry Sauce Cubes
CACHED false
EXECUTIONTIME 0
SQL SELECT * FROM allRecipes WHERE homepage_order > 0 ORDER BY homepage_order