ColdFusion使用一个提交插入多个表单域

时间:2014-09-22 16:58:50

标签: coldfusion coldfusion-9 sql-insert

要点:

我有一个CFC,可以成功地将一组表单字段插入到数据库表中。现在我想修改它以插入多个记录,但是当我尝试处理动态表单字段名称时出现错误。

详情:

我有(5)表单需要同时插入到db中的字段,作为不同的行。所以我循环并递增了字段编号。我为它创建了5个静态行。

<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
  • ...

我尝试通过循环将字段插入到数据库中:

<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构造

我尝试了各种各样的东西,却找不到合适的语法。任何建议将不胜感激。

1 个答案:

答案 0 :(得分:2)

变量的格式很接近但不完全存在,如果它们在引用块中,则只能对#进行双重换行。

'#Form["ITIProgramName#x#"]#'

我还建议在sql插入的变量周围使用cfqueryparam。

相关问题