Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

html form data to database

Status
Not open for further replies.

jvande

MIS
Jun 6, 2001
115
US
I am trying to post html form data to a database. I would like a way for it to loop through all the fields from the form and populate the fields in the database with that same name. For example the standard way to do it is as follows:
Set oRS = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT * FROM customersPDF"
oRS.Open strSQL, dbConn, 1, 3, 1
oRS.AddNew
oRS("fname") = request.form("fname")
oRS("lname") = request.form("lname")
oRS("SSN") = request.form("SSN")
oRS("address") = request.form("address")

Instead I would like it to work like this, psuedo code:
for i = 1 to 4
ors("database field name") = request.form("form field name")
next i
 
If you did something like the following you may be able to do what you want.

Dim fldF
Set oRS = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT * FROM customersPDF"
oRS.Open strSQL, dbConn, 1, 3, 1
' I don't use this... you don't need to if you have something there already. I'll explain more in a moment. oRS.AddNew
For Each fldF In oRS.Fields
oRS(fldF.Name) = Request(fldF.Name)
Next

I have not tested this yet, but it is an interesting solution if it works...

Now, as far as Adding a New Record. (The method above is great for updating.) I like to create a page with something like this. (theory, not code, here)

if New Entry then
addNew 1 field(usually a primary key that will never repeat)
end if
'Now update/insert code.
For Each fldF In oRS.Fields
oRS(fldF.Name) = Request(fldF.Name)
Next

So you basically have two DB calls. This serves a purpose, because you can use your update.record code for new records as well as when you update, instead of having to have all your field names listed twice in the code. This may be a bit much, and it is somewhat disjointed. Post back if you are confused.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top