Forums

ASP

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>&lt;%
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
%&gt;
&lt;%
Else

strSelectedFacility = Request.QueryString("facility"

arrSelectedFacility = Split(strSelectedFacility, ", ", -1, 1)
%&gt;
&lt;P class="text"&gt;You selected &lt;B&gt;&lt;%= intNumberSelected %&gt;&lt;/B&gt; team(s).&lt;/P&gt;
&lt;P class="text"&gt;Request.QueryString("facility" returned:&lt;/P&gt;
&lt;P class="text"&gt;&lt;FONT SIZE="-1"&gt;&lt;B&gt;&lt;%= strSelectedFacility %&gt;&lt;/B&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class="text"&gt;You can easily convert this to an array using the split command. The contents of that array are shown in the table below:&lt;/P&gt;
&lt;TABLE BORDER="1"&gt;
&lt;TR&gt;
&lt;TH class="text"&gt;Array Element &lt;FONT COLOR="#FF0000"&gt;*&lt;/FONT&gt;&lt;/TH&gt;
&lt;TH class="text"&gt;Value&lt;/TH&gt;
&lt;/TR&gt;
&lt;%
' 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)
%&gt;
&lt;TR&gt;
&lt;TD class="text"&gt;&lt;%= I %&gt;&lt;/TD&gt;
&lt;TD class="text"&gt;&lt;%= arrSelectedFacility(I) %&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;%
Next 'I
%&gt;
&lt;/TABLE&gt;
&lt;%
End If
%&gt;
&lt;%
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
%&gt;</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:
This is ASP but it may help??

www.webthang.co.uk/tuts/tuts_dmx/rob8/rob8.asp
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

Reply to this topic