Jump to content

ASP put split values from array into a table?


Recommended Posts

I'm having trouble trying to insert checkbox values into an access database. Basically, I've got a form with checkboxes in and checkbox values. It takes the checked checkbox values, then split them into an array. Now I want to put the array values into a table (on seperate rows, so multiple inserts at once). The table structure is

CustomerNumber & CustomerType which are both numbers. The customernumber is the same for each checkbox value inserted during the form submission.

Currently I can get this code to work to write out each checkbox value like this:

<%

CT= Request.Form("ctcheckbox")

myArray=Split(CT,",")

For i=LBound(myArray) to UBound(myArray)

Response.Write myArray(i)

Next%>

But I need to include it in this code to insert it into the database, but I don't know how to reference the array:

<%

if(Request.Form("Comnum") <> "") then Command1__CN = Request.Form("Comnum")

%>

<%set Command1 = Server.CreateObject("ADODB.Command")

Command1.ActiveConnection = MM_conn2_STRING

Command1.CommandText = "INSERT INTO CustomerTypes (CustomerNumber, CustomerType) VALUES (" + Replace(Command1__CN, "'", "''") + ",'myArray') "

Command1.CommandType = 1

Command1.CommandTimeout = 0

Command1.Prepared = true

Command1.Execute()

%>

Link to comment
Share on other sites

  • 2 weeks later...

edited out - jumped the gun here and misread a few sections. My apologies as I additionally know nothing about ASP here.

Afterthought: Have you tried referencing your array directly such as myarray(0) myarray(1) myarray(2) and so forth? Additonally, have you tried using an array conversion function to convert it into a string? Since the myarray in red seems to be sent as if you're trying to send the whole array at once.

There is a good tutorial on ASP arrays on

http://www.codefixer.com/tutorials/arrays.asp

http://www.codefixer.com/tutorials/array_functions.asp

Edited by Chozo4
Link to comment
Share on other sites

Two ways you could do it. Depends upon how dynamic you want this to be for future changes.

<%
myArray=Split(CT,",")
For i=LBound(myArray) to UBound(myArray)
AddValues = AddValue + ",'" + Response.Write myArray(i) + "'"
Next
set Command1 = Server.CreateObject("ADODB.Command")
Command1.ActiveConnection = MM_conn2_STRING
Command1.CommandText = "INSERT INTO CustomerTypes (CustomerNumber, CustomerType) VALUES (" + Replace(Command1__CN, "'", "''")" + AddValues +") "
Command1.CommandType = 1
Command1.CommandTimeout = 0
Command1.Prepared = true
Command1.Execute()
%>

or

<%
myArray=Split(CT,",")
set Command1 = Server.CreateObject("ADODB.Command")
Command1.ActiveConnection = MM_conn2_STRING
Command1.CommandText = "INSERT INTO CustomerTypes (CustomerNumber, CustomerType) VALUES (" + Replace(Command1__CN, "'", "''")" + ",'" +myArray(0) + "','" + myArray(1) +"') "
Command1.CommandType = 1
Command1.CommandTimeout = 0
Command1.Prepared = true
Command1.Execute()
%>

The first will let you add as many checkboxes as you like without have to keep adding myArray(x) to the end of the update query.

Note: I may have missed a quote here or there.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...