Writing the Data to the Database
October 16, 2000
Finishing the Job with ADO
All that remains is to get this data actually written to the database.
Before we move on to that, in case you did not know, you should put
the ADO related code we have been talking about around the validation
code, like so:
if request.form("isSubmitted") = "yes" then
dim conn, strSQL, rst
set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = "dsn=wdvl_test"
set rst = Server.CreateObject("ADODB.Recordset")
'...All the stuff in between...
'Email
re.Pattern = "^\w+@\w+\.\w+"
results = re.Test(email)
if results then
errorArray(8) = "False"
else
errorArray(8) = "True"
ErrorMsg = ErrorMsg & "Email Address<br>"
end if
'One of the problems with the original page is that
'it doesn't do anything with the form after it's validated!
'We add a check for ErrorMsg here (to ensure no errors)
'and then we actually do something with the data!
if ErrorMsg = "" then
strSQL = "SELECT * FROM tbl_users;"
rst.Open strSQL, conn, 3, 3
'There will be more to come here!
end if
end if
%gt;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
Etc...
Writing the data to the database is a simple matter that involves a
few ADO Recordset methods and a little manipulation. First, you need
to know how to add a new record to the database. In Access you would
just move to the next line in Datasheet view and just start typing.
On the web you need to specifically tell it to create the new record.
This task is done using the AddNew method of the Recordset and looks
like this: rst.AddNew. Following that easy command, you
will need to match up the database's field names to the web pages
stored versions of the data being submitted. If that does not make
sense, look at the following example:
'...
rst("First_Name") = first_name
'etc...
The rst("Field_Name") on the left-hand side of the equals
sign is a reference to the database field "First_Name" being made
through the Recordset we created. The variable on the right side of
the equals sign is the one created by the web page and assigned the
value of Request.Form("first_name"). You will need to do
this for each database field in the Recordset you created. Once you
are done, you need to commit those changes to the actual data source,
which is done by adding in the code: rst.Update. That is
it! The record is added into the database!
SQL and Recordsets
Part 3 - Building a Registration Database
Conclusion
|