473,789 Members | 2,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

For each item in Request.Form

I need to iterate through a submitted form, inserting data on each pass. In
the past, I have always used form elements that were named with numbers at
the end, like this,

name1 relationship1
name2 relationship2
name3 relationship3

With the above formatting, I could write a For... Next loop to insert all
the data into my database,

For i = 1 to 3
If Request.Form("n ame" & i) <> "" Then
sql = INSERT INTO Table (Name, Relationship)
sql = sql & " VALUES"
sql = sql & "('" Request.Form("n ame" & i) "',"
sql = sql & "('" Request.Form("r elationship" & i) "')"

'Execute SQL
End If
Next

Now I am working on a new loop, but do not want to go back and change the
way the submitting page works. I saw on while researching that I can use a
For each item in Request.Form, but tried this and it throws off my SQL
statement. It adds all of the request.form values to the sql statement,
which then fails.

How can I make this loop?

Thanks,
Drew
Jan 24 '06 #1
14 19948
Drew wrote:
I need to iterate through a submitted form, inserting data on each
pass. In the past, I have always used form elements that were named
with numbers at the end, like this,

name1 relationship1
name2 relationship2
name3 relationship3

With the above formatting, I could write a For... Next loop to insert
all the data into my database,

For i = 1 to 3
If Request.Form("n ame" & i) <> "" Then
sql = INSERT INTO Table (Name, Relationship)
sql = sql & " VALUES"
sql = sql & "('" Request.Form("n ame" & i) "',"
sql = sql & "('" Request.Form("r elationship" & i) "')"

'Execute SQL
End If
Next

Now I am working on a new loop, but do not want to go back and change
the way the submitting page works. I saw on while researching that I
can use a For each item in Request.Form, but tried this and it throws
off my SQL statement. It adds all of the request.form values to the
sql statement, which then fails.


Response.Write the statement to see why it fails. We cannot debug a sql
statement without seeing what it is.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jan 24 '06 #2
The SQL Statement will look like this, if there are 3 textboxes for name and
relationship,

INSERT INTO Table (Name,Relations hip) VALUES ('Jack, John, Diane','Brother ,
Brother, Sister')

I need it to look like this,

INSERT INTO Table (Name,Relations hip) VALUES ('Jack','Brothe r')
INSERT INTO Table (Name,Relations hip) VALUES ('John','Brothe r')
INSERT INTO Table (Name,Relations hip) VALUES ('Diane','Siste r')

Thanks,
Drew
"Bob Barrows [MVP]" <re******@NOyah oo.SPAMcom> wrote in message
news:ug******** ********@TK2MSF TNGP12.phx.gbl. ..
Drew wrote:
I need to iterate through a submitted form, inserting data on each
pass. In the past, I have always used form elements that were named
with numbers at the end, like this,

name1 relationship1
name2 relationship2
name3 relationship3

With the above formatting, I could write a For... Next loop to insert
all the data into my database,

For i = 1 to 3
If Request.Form("n ame" & i) <> "" Then
sql = INSERT INTO Table (Name, Relationship)
sql = sql & " VALUES"
sql = sql & "('" Request.Form("n ame" & i) "',"
sql = sql & "('" Request.Form("r elationship" & i) "')"

'Execute SQL
End If
Next

Now I am working on a new loop, but do not want to go back and change
the way the submitting page works. I saw on while researching that I
can use a For each item in Request.Form, but tried this and it throws
off my SQL statement. It adds all of the request.form values to the
sql statement, which then fails.


Response.Write the statement to see why it fails. We cannot debug a sql
statement without seeing what it is.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Jan 24 '06 #3
Drew wrote:
The SQL Statement will look like this, if there are 3 textboxes for
name and relationship,

INSERT INTO Table (Name,Relations hip) VALUES ('Jack, John,
Diane','Brother , Brother, Sister')


So this is the failing statement resulting from your code?
Let's see the code that generated this.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jan 24 '06 #4
If Request.Form("S ubmitted") = "Yes" Then
'Start Loop
For Each item In Request.Form
'Run Insert Code
sql="INSERT INTO People (Name,Relations hip)"
sql=sql & " VALUES "
sql=sql & "('" & Request.Form("N ame") & "',"
sql=sql & "'" & Request.Form("R elationship") & "')"

'execute the insert to SQL Server
Set MM_editCmd = Server.CreateOb ject("ADODB.Com mand")
MM_editCmd.Acti veConnection = MM_lcnn_STRING
MM_editCmd.Comm andText = sql
Response.Write( sql)
MM_editCmd.Exec ute
MM_editCmd.Acti veConnection.Cl ose
Next
End If

Thanks,
Drew
"Bob Barrows [MVP]" <re******@NOyah oo.SPAMcom> wrote in message
news:u7******** ********@tk2msf tngp13.phx.gbl. ..
Drew wrote:
The SQL Statement will look like this, if there are 3 textboxes for
name and relationship,

INSERT INTO Table (Name,Relations hip) VALUES ('Jack, John,
Diane','Brother , Brother, Sister')


So this is the failing statement resulting from your code?
Let's see the code that generated this.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Jan 24 '06 #5
Drew wrote on 24 jan 2006 in microsoft.publi c.inetserver.as p.general:
For Each item In Request.Form
'Run Insert Code
sql="INSERT INTO People (Name,Relations hip)"
sql=sql & " VALUES "
sql=sql & "('" & Request.Form("N ame") & "',"
sql=sql & "'" & Request.Form("R elationship") & "')"


I don't think this will work.

Do you expect multiple
Request.Form("N ame") and Request.Form("R elationship") groups
that will listen to "Each item In Request.Form" ???
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 24 '06 #6
Yep... as I say, I have never used the "For each item in Request.Form", I
thought it would iterate through each form element, instead it just comma
delimits them and that is it. I guess I will have to build an array in the
For... Next loop and insert the array values?

Thanks,
Drew

"Evertjan." <ex************ **@interxnl.net > wrote in message
news:Xn******** ************@19 4.109.133.242.. .
Drew wrote on 24 jan 2006 in microsoft.publi c.inetserver.as p.general:
For Each item In Request.Form
'Run Insert Code
sql="INSERT INTO People (Name,Relations hip)"
sql=sql & " VALUES "
sql=sql & "('" & Request.Form("N ame") & "',"
sql=sql & "'" & Request.Form("R elationship") & "')"


I don't think this will work.

Do you expect multiple
Request.Form("N ame") and Request.Form("R elationship") groups
that will listen to "Each item In Request.Form" ???
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Jan 24 '06 #7
Drew wrote:
If Request.Form("S ubmitted") = "Yes" Then
'Start Loop
For Each item In Request.Form
'Run Insert Code
sql="INSERT INTO People (Name,Relations hip)"
sql=sql & " VALUES "
sql=sql & "('" & Request.Form("N ame") & "',"
sql=sql & "'" & Request.Form("R elationship") & "')"


To see what is going on here, do a
Response.Write Request.Form("N ame")
See? the form submission process concatenates the values from all
like-minded fields into a comma-delimited string.

You need to revert to the technique of differentiating the field names with
"row" numbers. You can still use "for each":

dim sql,sName,sRela tionship, arParms
sql="INSERT INTO People (Name,Relations hip) Values(?,?)"
dim cn

'Always use an explicit connection object - implicit connections
'can impair performance by disabling session pooling

Set cn = Server.CreateOb ject("ADODB.Con nection")
cn.open MM_lcnn_STRING
Set MM_editCmd = Server.CreateOb ject("ADODB.Com mand")
Set MM_editCmd.Acti veConnection = cn
MM_editCmd.Comm andText = sql
MM_editCmd.comm andtype = 1 'adCmdText
For Each item In Request.Form
If left(item,4) = "Name" then
rownumber= mid(item,5)
sName=request.f orm(item)
sRelationship=r equest.form("Re lationship" & rownumber)
arParms=Array(s Name,sRelations hip)
MM_editCmd.Exec ute ,arParms, 128 'adExecuteNoRec ords
end if
Next
cn.close: set cn=nothing

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jan 24 '06 #8
If you could be sure the field values were always in the proper order (I am
not sure this is a valid assumption), you could do this:

dim arNames, arRelationships , i
arNames = Split(Request.F orm("Name"),"," )
arRelationships = Split(Request.F orm("Relationsh ip"),",")

dim sql,sName,sRela tionship, arParms
sql="INSERT INTO People (Name,Relations hip) Values(?,?)"
dim cn

'Always use an explicit connection object - implicit connections
'can impair performance by disabling session pooling

Set cn = Server.CreateOb ject("ADODB.Con nection")
cn.open MM_lcnn_STRING
Set MM_editCmd = Server.CreateOb ject("ADODB.Com mand")
Set MM_editCmd.Acti veConnection = cn
MM_editCmd.Comm andText = sql
MM_editCmd.comm andtype = 1 'adCmdText
for i = 0 to ubound(arNames)
arParms= Array(arNames(i ), arRelationships (i))
MM_editCmd.Exec ute ,arParms, 128 'adExecuteNoRec ords
Next
cn.close: set cn=nothing
Drew wrote:
Yep... as I say, I have never used the "For each item in
Request.Form", I thought it would iterate through each form element,
instead it just comma delimits them and that is it. I guess I will
have to build an array in the For... Next loop and insert the array
values?

Thanks,
Drew

"Evertjan." <ex************ **@interxnl.net > wrote in message
news:Xn******** ************@19 4.109.133.242.. .
Drew wrote on 24 jan 2006 in microsoft.publi c.inetserver.as p.general:
For Each item In Request.Form
'Run Insert Code
sql="INSERT INTO People (Name,Relations hip)"
sql=sql & " VALUES "
sql=sql & "('" & Request.Form("N ame") & "',"
sql=sql & "'" & Request.Form("R elationship") & "')"


I don't think this will work.

Do you expect multiple
Request.Form("N ame") and Request.Form("R elationship") groups
that will listen to "Each item In Request.Form" ???
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jan 24 '06 #9
What heppens if the persons name happens to be O'Brian ?

Or worse yet what happens if the user enters something like:-
',''); Delete People; Other SQL code that does malicious things; --

?? Ouch.

It's called SQL Injection.


"Drew" wrote:
Yep... as I say, I have never used the "For each item in Request.Form", I
thought it would iterate through each form element, instead it just comma
delimits them and that is it. I guess I will have to build an array in the
For... Next loop and insert the array values?

Thanks,
Drew

"Evertjan." <ex************ **@interxnl.net > wrote in message
news:Xn******** ************@19 4.109.133.242.. .
Drew wrote on 24 jan 2006 in microsoft.publi c.inetserver.as p.general:
For Each item In Request.Form
'Run Insert Code
sql="INSERT INTO People (Name,Relations hip)"
sql=sql & " VALUES "
sql=sql & "('" & Request.Form("N ame") & "',"
sql=sql & "'" & Request.Form("R elationship") & "')"


I don't think this will work.

Do you expect multiple
Request.Form("N ame") and Request.Form("R elationship") groups
that will listen to "Each item In Request.Form" ???
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Jan 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
10336
by: Christopher Brandsdal | last post by:
Hi! I get an error when I run my code Is there any other way to get te information from my form? Heres the error I get and the code beneath. Line 120 is market with ''''''''''''Line 120''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
2
2688
by: Laurent Bertin | last post by:
Hi i got a strange problem but it's true i don't make thing like anyone... First Config: + IIS5.0 SP2 (yes i know...) WebSite Security Root : Digest Authentication, NT Authenticated SubFolders : Anonymous Login Anonymous login is set to use a domain user to enable a sql server authenticated connection. Permissions are based on Page/action/user Membership
9
21496
by: Dan | last post by:
I am trying to use Request.Form("__EVENTTARGET") to get the name of the control that caused a post back. It keeps returning "". I am not really sure why, this happens for all of my controls that invoke are invoking a post back. I've never used this type of method before, but I need to get the name of the control doing the postback in the Form Load event, and cannot wait until the event of the target control that runs due to the...
10
3478
by: Mr Newbie | last post by:
DropDown lists and Listboxes do not appear in the list of controls and values passed back to the server on PostBack in Request.Form object. Can someone confirm this to be correct and possibly answer why ? The impact of this is that values added via javascript will never make it back to the server side code unless one does some jiggery pokery with hidden fields etc. If this is true, then I would like to understand the reason behind the...
8
3088
by: abcd | last post by:
I can get the value on the form at the server side by using Request.form("max") when max field is disabled I dont get value. For GUI and business logic purpose I have disabled some fields with the values, but when I update Request.form does not return me anything for disabled fields. How to read disabled fields at the server side
4
3769
by: Michael Kujawa | last post by:
I am using the following to create an SQL statement using the names and values from request.form. The loop goes through each item in request.form The issue comes in having an additional "and" at the end of the loop and the value of x3 as not all options may be selected from the form yet the loop goes through the entire request.form list I have to add addtional code to strip off the last "and" and was wondering if there is a way to...
1
3902
by: Jeremy Fourman | last post by:
Hello, I am in need of parsing a page with a few text boxes and an option list. The current state of the input fields are <input type="text"> there are no id or name attributes, however the option list does have a name value. When I run this: For Each Item in Request.Form Response.Write(Item & "=" & Request.Form(Item) ) Next I recieve only the option list name and selected option value. Is there a way to parse an input that...
5
5906
by: =?Utf-8?B?QVRT?= | last post by:
PRB JavaScript in ASP with Request.Form Please help, I'm having a problem with JavaScript in ASP, where the ASP page crashes when I try to determine if data was posted from a FORM or through a QueryString. Where, if the data came through via a FORM, I want to use it 1st, but if not, to then try using it from the QueryString. Basically, the problem boils down to using "toString()" on Request.Form("NAME") and Request.QueryString("NAME")...
7
9662
by: =?Utf-8?B?QVRT?= | last post by:
HOWTO Make CStr for JavaScript on ASP w/ Request.Form and QueryString In ASP, Request.Form and Request.QueryString return objects that do not support "toString", or any JavaScript string operation on parameters not passed. Example: Make a TST.asp and post to it as TST.asp?STATE=TEST <%@ Language=JavaScript %> <%
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.