We help IT Professionals succeed at work.
Get Started

How do I insert multiple values into seperate database rows using checkbox

Last Modified: 2013-12-16
I have 3 tables in a database (client, varietal, clientvarietal)

I have an admin form that generates multiple checkbox's from the varietal table (varietal_name) that the admin selects when adding a new client (So a client gets associated with one or more varietals).

Everything works fine when you add a client and only check one checkbox.  But when the user selects more than one checkbox CF throws this error: Conversion failed when converting the varchar value '3,4' to data type int.
It looks like the checkbox values are being outputed into a list and trying to be inserted into one row of the clientvariable table.

How would you write the code so that it only puts one value per row?

Here is the relevant code for clarification.
-----------Code from form -----------
<cfquery name="qlistvarietal" datasource="localinfo">
SELECT varietalID, varietal_name FROM varietal
<cfform action="Add_Client_Action.cfm" name="TheForm" onclick="return checkEmail()" method="post" enctype="multipart/form-data">
<td colspan="3">First Name:</td><td colspan="3"><cfinput type="text" name="txtFirstName" required="yes" message="Please enter a first name."></td><td colspan="3">Last Name:</td><td colspan="3"><cfinput type="text" name="txtLastName" required="yes" message="Please enter a last name."></td>
<td>Varietals:</td><cfoutput query="qlistvarietal">
<td>#varietal_name#:</td><td><cfinput type="checkbox" name="chkvarietal" value="#varietalID#"></td></cfoutput>
<td><cfinput type="submit" name="submit" value="Add Client"></td>

----------------code from action page----------------

<cfquery name="qaddclient" result="qresult">
(client_firstname, client_lastname) VALUES ('#Form.txtFirstName#', '#Form.txtLastName#')
 <cfquery name="qgetclient">
FROM client

<cfquery name="qaddclientwithvarietal">
INSERT INTO clientvarietal
(clientID, varietalID) VALUES ('#qgetclient.clientID#', '#Form.chkvarietal#')

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 31 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE