ColdFusion 一次提交即可插入多个表单域 [英] ColdFusion Insert multiple form fields with one submit
问题描述
总结:
我有一个 CFC,它成功地将一组表单字段插入到数据库表中.现在我想修改它以插入多条记录,但是当我尝试处理动态表单字段名称时出现错误.
I have a CFC that successfully inserts a single set of form fields into a database table. Now I want to modify it to insert multiple records, but am getting an error when I try to process the dynamic form field names.
详情:
我有 (5) 个表单字段需要同时插入到数据库中,作为不同的行.所以我循环并增加了字段编号.为了简单起见,我创建了 5 个静态行.
I have (5) form fields that need to be inserted in to the db at the same time, as different rows. So I looped and incremented the field number. I created 5 static rows for it to be simple.
<cfloop index="x" from="1" to="5">
<cfoutput>
<input name="ITIProgramName#x#" type="text" ..>
<input name="ITIProgVer#x#" type="text" ..>
</tr>
</cfoutput>
</cfloop>
所以结果是:
- ITIPROGRAMNAME1,ITIProgVer1
- ITIPROGRAMNAME2、ITIProgVer2
- ITIPROGRAMNAME3、ITIProgVer3
- ...
我尝试通过这样的循环将字段插入数据库:
I tried to insert the fields into the database by looping like so:
<cfloop from="1" to="5" index="x" >
<cfquery datasource="ITSReporting" name="InsertQuery">
INSERT INTO ITIPRO
( ServerID,
ServerName,
ProgramName,
CurrentProgVer,
LastUser,
UpDone )
VALUES
( '#Form.ServerID#',
'#Form.ServerName#',
'#Form[ITIProgramName#x#]#',
'#Form[ITIProgVer#x#]#',
'#CGI.Auth_User#',
#CreateODBCDateTime(Now())#
)
</cfquery>
</cfloop>
但它会产生错误
CFML 构造无效
我尝试了各种方法,但找不到正确的语法.任何建议将不胜感激.
I've tried all sorts of things, but cannot find the right syntax. Any suggestions would be much appreciated.
推荐答案
你的变量的格式很接近但不完全在那里,如果它们在引号块中,你只能将 # 双重包装.
The format of your variable is close but not quite there, you can only double wrap the # if they are in a quote block.
'#Form["ITIProgramName#x#"]#'
我还建议在 sql 插入的变量周围使用 cfqueryparam.
I would also suggest using a cfqueryparam around your variables on sql inserts.
这篇关于ColdFusion 一次提交即可插入多个表单域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!