We help IT Professionals succeed at work.
Get Started

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

535 Views
Last Modified: 2013-12-16
Hi,
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.
Thanks
-----------Code from form -----------
<cfquery name="qlistvarietal" datasource="localinfo">
SELECT varietalID, varietal_name FROM varietal
</cfquery>
<cfform action="Add_Client_Action.cfm" name="TheForm" onclick="return checkEmail()" method="post" enctype="multipart/form-data">
<table>
<tr>
<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>
</tr>
<td>Varietals:</td><cfoutput query="qlistvarietal">
<td>#varietal_name#:</td><td><cfinput type="checkbox" name="chkvarietal" value="#varietalID#"></td></cfoutput>
</tr>
<tr>
<td><cfinput type="submit" name="submit" value="Add Client"></td>
</tr>
</table>
</cfform>

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

<cftransaction>
<cfquery name="qaddclient" result="qresult">
INSERT INTO client
(client_firstname, client_lastname) VALUES ('#Form.txtFirstName#', '#Form.txtLastName#')
</cfquery>
 <cfquery name="qgetclient">
SELECT TOP 1 * 
FROM client
ORDER BY clientID DESC
</cfquery>

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

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Commented:
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