Insert multiple records from multiple checkboxes on a form (coldfusion)

Discussion in 'Programming' started by brazenmedia, Sep 27, 2006.

  1. #1
    Hello,

    Very green with coldfusion....so be gentle. Also this form was created with Dreamweaver 8 server behaviors so it may not be efficient.

    I need to insert multiple records into a database from a form with Coldfusion and am stuck – it functions properly when inserting one record at a time. Basically the user will select their employee name (EmployeeID) from a dynamic list, and then multiple session numbers (SessionNumber) which are training classes and submit.

    When I try and select multiple checkboxes I get this error:
    ‘Number of query values and destination fields are not the same.’

    I have all of the checkboxes named the same as you can see, and I'm guessing they need to be different or have special ID's to process. I think I also need to loop through the queries on the form elements but don't know how to do either properly. I've tried using the <cfloop> tag to no avail...

    Can someone help with the code below to insert the 'SessionNumber' field as many times as it is selected via checkboxes on the form?




    ----APPLICATION CODE------------------------------------------------------------------

    <!--Get Employee Names -->
    <cfquery name="rsDisplayEmployee" datasource="hrtraining">
    SELECT *
    FROM tblEmployees
    ORDER BY LastName ASC</cfquery>
    <!-- -->
    <!--Insert Records -->
    <cfset CurrentPage=GetFileFromPath(GetTemplatePath())>
    <cfif IsDefined("FORM.MM_InsertRecord") AND FORM.MM_InsertRecord EQ "form1">
    <cfquery datasource="hrtraining">
    INSERT INTO tblEnrollments (SessionNumber, EmployeeID)
    VALUES (
    <cfif IsDefined("FORM.SessionNumber") AND #FORM.SessionNumber# NEQ "">
    #FORM.SessionNumber#
    <cfelse>
    NULL
    </cfif>
    ,
    <cfif IsDefined("FORM.EmployeeID") AND #FORM.EmployeeID# NEQ "">
    #FORM.EmployeeID#
    <cfelse>
    NULL
    </cfif>
    )</cfquery>
    <cflocation url="2.cfm">
    </cfif>
    <!-- -->

    --------FORM CODE-----------------------------------------------------------------------

    <form action="<cfoutput>#CurrentPage#</cfoutput>" id="form" name="form" method="POST">
    <table width="100%" border="0" cellspacing="0" cellpadding="4">
    <tr>
    <td width="100%"><h2><strong>Select Employee Name </strong></h2></td>
    </tr>
    <tr>
    <td><select name="EmployeeID">
    <cfoutput query="rsDisplayEmployee">
    <option value="#rsDisplayEmployee.EmployeeID#">#rsDisplayEmployee.LastName#, #rsDisplayEmployee.FirstName#</option>
    </cfoutput>
    </select></td>
    </tr>
    </table>
    <h2><strong>Select Training Class </strong></h2>
    <table width="600" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="227" valign="top">Class</td>
    <td width="373" valign="top"><input name="SessionNumber" type="checkbox" value="937" />
    Date</td>
    </tr>
    <tr>
    <td valign="top">Class</td>
    <td valign="top"><label>
    <input name="SessionNumber" type="checkbox" value="936" />
    </label>
    Date</td>
    </tr>
    </table>
    <br />
    <input type="submit" name="Submit" value="Sign Up!" />
    <input type="hidden" name="MM_InsertRecord" value="form">
    </form>
     
    brazenmedia, Sep 27, 2006 IP
  2. brazenmedia

    brazenmedia Active Member

    Messages:
    311
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    93
    #2
    I figured this one out....um - thanks me?
     
    brazenmedia, Sep 28, 2006 IP