Forums
This topic is locked
Insert checkbox data into MSSQL table
Posted 10 May 2005 13:36:15
1
has voted
10 May 2005 13:36:15 James Wilkinson posted:
I'm using ASP to insert form data into a MSSQL table.I've been trying to do this for a day now with no luck, so please help.
I have a form which has about 10 checkboxes on it, and asks the user to select all that apply, all have a numerical value.
When the data is passed it looks like: 1100, 1101, 1104, 1201, 1203 depending on what was selected.
I can insert this into one row in a table, but what i need to do is split this up and insert it into individual rows.
I'm not sure if i'm going in the right direction but i've managed to get it into an array and list the values, like so:
1100
1101
1104
1201
1203
I got the code to turn them into an array from somewhere else and dont understand it to well.
But i cant insert them into new rows into the table. Here is my code on the page after the form, starting with the array code and at the bottom it creates a new table and then inserts the checkbox data.
<pre id=code><font face=courier size=2 id=code><%
Dim intNumberSelected ' Count of items selected
Dim strSelectedFacility ' String returned from QS (or Form)
Dim arrSelectedFacility ' Variable to hold team array
Dim I ' Looping variable
' Retrieve the count of items selected
intNumberSelected = Request.QueryString("facility".Count
If intNumberSelected = 0 Then
%>
<%
Else
strSelectedFacility = Request.QueryString("facility"
arrSelectedFacility = Split(strSelectedFacility, ", ", -1, 1)
%>
<P class="text">You selected <B><%= intNumberSelected %></B> team(s).</P>
<P class="text">Request.QueryString("facility" returned:</P>
<P class="text"><FONT SIZE="-1"><B><%= strSelectedFacility %></B></FONT></P>
<P class="text">You can easily convert this to an array using the split command. The contents of that array are shown in the table below:</P>
<TABLE BORDER="1">
<TR>
<TH class="text">Array Element <FONT COLOR="#FF0000">*</FONT></TH>
<TH class="text">Value</TH>
</TR>
<%
' Some debugging lines if you start having problems
'Response.Write LBound(arrSelectedTeams)
'Response.Write UBound(arrSelectedTeams)
' Loop through the array showing one table row for each selection
For I = LBound(arrSelectedFacility) To UBound(arrSelectedFacility)
%>
<TR>
<TD class="text"><%= I %></TD>
<TD class="text"><%= arrSelectedFacility(I) %></TD>
</TR>
<%
Next 'I
%>
</TABLE>
<%
End If
%>
<%
MM_editQuery = "CREATE TABLE FacilityTypes (FacilityType int)"
Set MM_editCmd = Server.CreateObject("ADODB.Command"
MM_editCmd.ActiveConnection = MM_connHMGuide_STRING
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editQuery = "INSERT INTO FacilityTypes (FacilityType)"
MM_editQuery = MM_editQuery & "VALUES ( '" & Session("sfacility" & "' );" ####this is where it inserts the checkbox data but it won't work if the user selects more than 1 checkbox######
Set MM_editCmd = Server.CreateObject("ADODB.Command"
MM_editCmd.ActiveConnection = MM_connHMGuide_STRING
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
%></font id=code></pre id=code>
Any help will be very appreciated.
Replies
Replied 11 May 2005 06:21:35
11 May 2005 06:21:35 aspdev05 dwmx04 replied:
Replied 27 May 2005 19:01:22
27 May 2005 19:01:22 John Lee replied:
ok, this is what I will do:
create a insert page called: insert_values.asp
then post the form to this page
under insert_value.asp code seems like this:
(if your chechbox is name checkbox1)
Dim i
for i=1 to Request.form("checkbox1".count
Set MM_editCmd = Server.CreateObject("ADODB.Command"
MM_editCmd.ActiveConnection = MM_connHMGuide_STRING
MM_editCmd.CommandText = "Insert into your_table (your_field,....,...) as ("&Request.form("checkbox1"(i)&",...,...)";"
MM_editCmd.Execute
Next
I think this is easier...
John Lee
create a insert page called: insert_values.asp
then post the form to this page
under insert_value.asp code seems like this:
(if your chechbox is name checkbox1)
Dim i
for i=1 to Request.form("checkbox1".count
Set MM_editCmd = Server.CreateObject("ADODB.Command"
MM_editCmd.ActiveConnection = MM_connHMGuide_STRING
MM_editCmd.CommandText = "Insert into your_table (your_field,....,...) as ("&Request.form("checkbox1"(i)&",...,...)";"
MM_editCmd.Execute
Next
I think this is easier...
John Lee