ColdFusion 更高效地处理表单并出现小错误
ColdFusion processing a form more efficiently and a minor error
我有一个我确信可以更有效地完成的表单处理,结果集中有一个错误,虽然不是 "life threatening" 只是不正确。
该页面的目的是将一个项目与一个程序相关联,同时在程序中关联一个名称 -- B
和 F
字段是允许项目的复选框与该特定程序中的多个程序和名称相关联。 (为清楚起见进行了编辑)
示例:
物品:光剑
- 计划:绝地训练
- 指定:工具(b)(是)
名称: 武器(f) (无)
程序:绝地大师
- 指定:工具(b)(是)
名称:武器(f)(是)
程序:走私者
- 指定:工具(b)(无)
- 名称:武器(f)(无)
形式:
<form action="#CGI.SCRIPT_NAME#" method="post" name="program">
<label>Item</label>
<select id="item" name="item">
<!---//loop through and display items --->
<cfloop query="getitems">
<option value="#itemid#">#itemname#</option>
</cfloop>
</select>
<table>
<!---//loop through and display programs --->
<cfloop query="getprogram">
<tr>
<td>#programname#</td>
<td><input type="checkbox" id="B#programid#" name="B#programid#"></td>
<td><input type="checkbox" id="F#programid#" name="F#programid#"></td>
</tr>
</cfloop>
</table>
<input type="submit">
</form>
操作页面:
<!---// is there is a form being processed --->
<cfif #CGI.REQUEST_METHOD# is 'post'>
<!---// create program list from query --->
<cfset pl = ValueList(query.var, ','>
<!---// set addtl form var's --->
<cfset listassid = 'form.item'>
<!---// loop over program list --->
<cfloop list="#pl#" index="i">
<!---// loop over form fields --->
<cfloop list="form.fieldnames" index="field">
<cfif #field# EQ 'B'&#i#>
<!---// if field is B and var, set designation true --->
<cfset b = 1>
<cfelse>
<!---// it's not, set to null --->
<cfset b = 'null'>
</cfif>
<cfif #field# EQ 'F'&#i#>
<!---// if field is F and var, set designation true --->
<cfset f = 1>
<cfelse>
<!---// it's not, set to null --->
<cfset f = 'null'>
</cfif>
<cfif b EQ 'null' AND f EQ 'null'>
<!---// if both are null then skip --->
//do nothing
<cfelse>
<!---//insert record into table --->
insert into table (table fields)
(#i#, #listassid#, #b#, #f# )
</cfif>
</cfloop>
</cfloop>
</cfif>
结果应该是:
id item_id program_id B F
1 24 1 x
2 32 2 x x
实际结果是:
id item_id program_id B F
1 24 1 x
2 32 2 x
3 32 2 x
提前感谢您提出的任何澄清和效率建议。
我认为稍微不同的命名约定将有助于解决当前问题,并提高代码的可读性。
一个选项是将所有项目 ID 存储在隐藏的表单字段中。请务必使用相同的 "name",以便将 ID 作为 CSV 列表提交。此外,我会为复选框推荐更有意义的名称,以提高可读性。例如,"isSomething" 而不仅仅是 "B" 或 "F":
<select id="item" name="itemID">
<cfoutput query="getitems">
<option value="#itemid#">#itemname#</option>
</cfoutput>
</select>
...
<cfoutput query="getPrograms">
<input type="hidden" name="programIDList" value="#programID#">
...
<!--- Set checkbox values to 1 / Yes --->
<td><input type="checkbox" name="isTool_#programID#" value="1"></td>
<td><input type="checkbox" name="isWeapon_#programID#" value="1"></td>
...
</cfoutput>
提交表单后,遍历项目 ID 列表并使用当前 ID 提取每组复选框的值。如果选中任一框,则将记录插入数据库。
* 注意:复选框只有在"checked"时才会提交。在访问该字段之前,请使用 structKeyExists to verify the field exists OR use cfparam 为该字段设置默认值。
<!--- Loop through list of available program id's --->
<cfloop list="#form.programIDList#" index="variables.programID">
<!--- Ensure fields exist. Set default value = 0 / No --->
<cfparam name="form.isTool_#variables.programID#" default="0">
<cfparam name="form.isWeapon_#variables.programID#" default="0">
<!--- Extract the current values --->
<cfset variables.isTool = FORM["isTool_"& variables.programID ] >
<cfset variables.isWeapon = FORM["isWeapon_"& variables.programID ] >
<!--- DEBUG ONLY: Display current values --->
<cfoutput>
<hr>variables.programID = #variables.programID#
<br>variables.isTool = #variables.isTool#
<br>variables.isWeapon = #variables.isWeapon#
<br>form.itemID = #form.itemID#
</cfoutput>
<!--- If either box was checked, save to database --->
<cfif variables.isTool || variables.isWeapon>
... run cfquery here
</cfif>
</cfloop>
关于数据库查询的两个重要说明:
从不 在 SQL 中使用原始客户端值。而是使用 cfqueryparam。它有助于保护您的数据库免受 sql 注入。它还可以在多次执行一条语句时提高查询性能(如本例)
执行多个(相关)查询时,一定要将它们包装在cftransaction to ensure consistency, ie All statements succeed or fail as a unit.
中
我有一个我确信可以更有效地完成的表单处理,结果集中有一个错误,虽然不是 "life threatening" 只是不正确。
该页面的目的是将一个项目与一个程序相关联,同时在程序中关联一个名称 -- B
和 F
字段是允许项目的复选框与该特定程序中的多个程序和名称相关联。 (为清楚起见进行了编辑)
示例:
物品:光剑
- 计划:绝地训练
- 指定:工具(b)(是)
名称: 武器(f) (无)
程序:绝地大师
- 指定:工具(b)(是)
名称:武器(f)(是)
程序:走私者
- 指定:工具(b)(无)
- 名称:武器(f)(无)
形式:
<form action="#CGI.SCRIPT_NAME#" method="post" name="program">
<label>Item</label>
<select id="item" name="item">
<!---//loop through and display items --->
<cfloop query="getitems">
<option value="#itemid#">#itemname#</option>
</cfloop>
</select>
<table>
<!---//loop through and display programs --->
<cfloop query="getprogram">
<tr>
<td>#programname#</td>
<td><input type="checkbox" id="B#programid#" name="B#programid#"></td>
<td><input type="checkbox" id="F#programid#" name="F#programid#"></td>
</tr>
</cfloop>
</table>
<input type="submit">
</form>
操作页面:
<!---// is there is a form being processed --->
<cfif #CGI.REQUEST_METHOD# is 'post'>
<!---// create program list from query --->
<cfset pl = ValueList(query.var, ','>
<!---// set addtl form var's --->
<cfset listassid = 'form.item'>
<!---// loop over program list --->
<cfloop list="#pl#" index="i">
<!---// loop over form fields --->
<cfloop list="form.fieldnames" index="field">
<cfif #field# EQ 'B'&#i#>
<!---// if field is B and var, set designation true --->
<cfset b = 1>
<cfelse>
<!---// it's not, set to null --->
<cfset b = 'null'>
</cfif>
<cfif #field# EQ 'F'&#i#>
<!---// if field is F and var, set designation true --->
<cfset f = 1>
<cfelse>
<!---// it's not, set to null --->
<cfset f = 'null'>
</cfif>
<cfif b EQ 'null' AND f EQ 'null'>
<!---// if both are null then skip --->
//do nothing
<cfelse>
<!---//insert record into table --->
insert into table (table fields)
(#i#, #listassid#, #b#, #f# )
</cfif>
</cfloop>
</cfloop>
</cfif>
结果应该是:
id item_id program_id B F
1 24 1 x
2 32 2 x x
实际结果是:
id item_id program_id B F
1 24 1 x
2 32 2 x
3 32 2 x
提前感谢您提出的任何澄清和效率建议。
我认为稍微不同的命名约定将有助于解决当前问题,并提高代码的可读性。
一个选项是将所有项目 ID 存储在隐藏的表单字段中。请务必使用相同的 "name",以便将 ID 作为 CSV 列表提交。此外,我会为复选框推荐更有意义的名称,以提高可读性。例如,"isSomething" 而不仅仅是 "B" 或 "F":
<select id="item" name="itemID">
<cfoutput query="getitems">
<option value="#itemid#">#itemname#</option>
</cfoutput>
</select>
...
<cfoutput query="getPrograms">
<input type="hidden" name="programIDList" value="#programID#">
...
<!--- Set checkbox values to 1 / Yes --->
<td><input type="checkbox" name="isTool_#programID#" value="1"></td>
<td><input type="checkbox" name="isWeapon_#programID#" value="1"></td>
...
</cfoutput>
提交表单后,遍历项目 ID 列表并使用当前 ID 提取每组复选框的值。如果选中任一框,则将记录插入数据库。
* 注意:复选框只有在"checked"时才会提交。在访问该字段之前,请使用 structKeyExists to verify the field exists OR use cfparam 为该字段设置默认值。
<!--- Loop through list of available program id's --->
<cfloop list="#form.programIDList#" index="variables.programID">
<!--- Ensure fields exist. Set default value = 0 / No --->
<cfparam name="form.isTool_#variables.programID#" default="0">
<cfparam name="form.isWeapon_#variables.programID#" default="0">
<!--- Extract the current values --->
<cfset variables.isTool = FORM["isTool_"& variables.programID ] >
<cfset variables.isWeapon = FORM["isWeapon_"& variables.programID ] >
<!--- DEBUG ONLY: Display current values --->
<cfoutput>
<hr>variables.programID = #variables.programID#
<br>variables.isTool = #variables.isTool#
<br>variables.isWeapon = #variables.isWeapon#
<br>form.itemID = #form.itemID#
</cfoutput>
<!--- If either box was checked, save to database --->
<cfif variables.isTool || variables.isWeapon>
... run cfquery here
</cfif>
</cfloop>
关于数据库查询的两个重要说明:
从不 在 SQL 中使用原始客户端值。而是使用 cfqueryparam。它有助于保护您的数据库免受 sql 注入。它还可以在多次执行一条语句时提高查询性能(如本例)
执行多个(相关)查询时,一定要将它们包装在cftransaction to ensure consistency, ie All statements succeed or fail as a unit.
中