Answered
Multiple Checkbox Insert as Multiple Records
I am relatively new to ColdFusion. It's simplicity, along
with Dreamweaver's insert/update/delete wizards, have allowed me to
build some pretty neat applications for basic data insertion,
retrieval and deletion and display.
However, I need to be able to insert multiple selections from checkboxes as multiple records in a table, and I'm running into problems. I'm guessing this sort of thing is the basic premise of pretty much any shopping cart out there, but I can't seem to find any good information on this in either my "Developing with CFMX Manual" or my "CFMX Web App. Construction Kit" book, or the web for that matter. (The only examples shown for either the form page or the action page are for one, static checkbox!!!)
The basic premise of what I'm trying to achieve:
User is able to select various items from multiple checkboxes. (ITEM_ID)
This selection is associated with the user. (CUSTOMER_ID)
The selection is associated with a point in time such as a year. (INSTANCE_ID)
These IDs all need to be inserted into a 3 column table expecting integers for ITEM_ID, CUSTOMER_ID, and INSTANCE_ID
Example of FORM Values that would be passed: ( 147,148,149 , 23445 , 4 ) = (3 ITEM_IDs, CUSTOMER_ID, and INSTANCE_ID)
Typical Error upon submission to insert action page: Count of read-write columns does not equal count of values (Which is to be expected, I'm gathering multiple values via the checkboxes).
How can I insert each checkbox selection as a separate record in the table???
Such as:
( 147 , 23445 , 4 )
( 148 , 23445 , 4 )
( 149 , 23445 , 4 )
My Code:
Form:
<form method="post" name="form2" action="process_form_action_page.cfm">
<table>
<cfoutput query="Stored_Procedure1">
<tr>
<td><input type="checkbox" name="ITEM_ID" value="#Stored_Procedure1.ITEM_ID#"></td>
<td>#Stored_Procedure1.ITEM_TITLE#</td>
</tr>
</cfoutput>
</table>
<input type="hidden" name="CUSTOMER_ID" value="#CUSTOMERS.CUSTOMER_ID#">
<input type="hidden" name="INSTANCE_ID" value="#INSTANCE.INSTANCE_ID#">
<input type="submit" value="Insert record">
</form>
Action Page:
<cfquery datasource="ORDERS">
INSERT INTO PRODUCT_CUSTOMER (ITEM_ID, CUSTOMER_ID, INSTANCE_ID) VALUES (
<cfif IsDefined("FORM.ITEM_ID") AND #FORM.ITEM_ID# NEQ "">
#FORM.ITEM_ID#
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.CUSTOMER_ID") AND #FORM.CUSTOMER_ID# NEQ "">
#FORM.CUSTOMER_ID#
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.INSTANCE_ID") AND #FORM.INSTANCE_ID# NEQ "">
#FORM.INSTANCE_ID#
<cfelse>
NULL
</cfif>
)
</cfquery>
</cfif>
Thanks!
However, I need to be able to insert multiple selections from checkboxes as multiple records in a table, and I'm running into problems. I'm guessing this sort of thing is the basic premise of pretty much any shopping cart out there, but I can't seem to find any good information on this in either my "Developing with CFMX Manual" or my "CFMX Web App. Construction Kit" book, or the web for that matter. (The only examples shown for either the form page or the action page are for one, static checkbox!!!)
The basic premise of what I'm trying to achieve:
User is able to select various items from multiple checkboxes. (ITEM_ID)
This selection is associated with the user. (CUSTOMER_ID)
The selection is associated with a point in time such as a year. (INSTANCE_ID)
These IDs all need to be inserted into a 3 column table expecting integers for ITEM_ID, CUSTOMER_ID, and INSTANCE_ID
Example of FORM Values that would be passed: ( 147,148,149 , 23445 , 4 ) = (3 ITEM_IDs, CUSTOMER_ID, and INSTANCE_ID)
Typical Error upon submission to insert action page: Count of read-write columns does not equal count of values (Which is to be expected, I'm gathering multiple values via the checkboxes).
How can I insert each checkbox selection as a separate record in the table???
Such as:
( 147 , 23445 , 4 )
( 148 , 23445 , 4 )
( 149 , 23445 , 4 )
My Code:
Form:
<form method="post" name="form2" action="process_form_action_page.cfm">
<table>
<cfoutput query="Stored_Procedure1">
<tr>
<td><input type="checkbox" name="ITEM_ID" value="#Stored_Procedure1.ITEM_ID#"></td>
<td>#Stored_Procedure1.ITEM_TITLE#</td>
</tr>
</cfoutput>
</table>
<input type="hidden" name="CUSTOMER_ID" value="#CUSTOMERS.CUSTOMER_ID#">
<input type="hidden" name="INSTANCE_ID" value="#INSTANCE.INSTANCE_ID#">
<input type="submit" value="Insert record">
</form>
Action Page:
<cfquery datasource="ORDERS">
INSERT INTO PRODUCT_CUSTOMER (ITEM_ID, CUSTOMER_ID, INSTANCE_ID) VALUES (
<cfif IsDefined("FORM.ITEM_ID") AND #FORM.ITEM_ID# NEQ "">
#FORM.ITEM_ID#
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.CUSTOMER_ID") AND #FORM.CUSTOMER_ID# NEQ "">
#FORM.CUSTOMER_ID#
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.INSTANCE_ID") AND #FORM.INSTANCE_ID# NEQ "">
#FORM.INSTANCE_ID#
<cfelse>
NULL
</cfif>
)
</cfquery>
</cfif>
Thanks!
