ColdFusion Insert multiple form fields with one su

2020-04-17 08:15发布

问题:

Summary:

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.

Detail:

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>

So the result is:

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

But it produces the Error

Invalid CFML construct

I've tried all sorts of things, but cannot find the right syntax. Any suggestions would be much appreciated.

回答1:

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#"]#'

I would also suggest using a cfqueryparam around your variables on sql inserts.