Answered
Insert Multiple records from one form with checkboxes
Hello,
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.
When I try and select multiple checkboxes I get this error:
‘Number of query values and destination fields are not the same.’
The SessionNumber field checkboxes are not dynamic as you can see – how do I use CFLOOP or any other method to allow multiple checkboxes to be selected and all SessionNumber records entered into the database on one form submit?
I have used Dreamweaver 8 server behaviors to build the form – which is limiting I know but I’m not a coder. Hopefully I can add some code here to make things function properly.
----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>
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.
When I try and select multiple checkboxes I get this error:
‘Number of query values and destination fields are not the same.’
The SessionNumber field checkboxes are not dynamic as you can see – how do I use CFLOOP or any other method to allow multiple checkboxes to be selected and all SessionNumber records entered into the database on one form submit?
I have used Dreamweaver 8 server behaviors to build the form – which is limiting I know but I’m not a coder. Hopefully I can add some code here to make things function properly.
----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>