PART II : Storing the Data
We’ll begin by adding an HTML form to listing.asp (previously created in Part I)
Here’s the code:
<% Option Explicit %>
<!-- #INCLUDE FILE="DBInfo.asp" -->
<HTML>
<HEAD><TITLE>Users listing</TITLE></HEAD>
<BODY>
<%
Dim objRS
Set objRS = Server.CreateObject ("ADODB.Recordset")
objRS.Open "Users", strConnect, adOpenForwardOnly, adLockReadOnly, adCmdTable
response.write "<table width='400' border='0' cellpadding='2'>
<tr bgcolor='#CCD6E0'><td><b>User ID</b></td>" & _
"<td><b>Full Name</b></td><td><b>Email</b></td></tr>"
While Not objRS.EOF
Response.Write "<tr><td>"
Response.Write objRS("ID") & "</td><td>" &objRS("Name") & " "&objRS("Lastname")& _
"</td><td>"&objRS("Email")&"</td></tr>"
objRS.MoveNext
Wend
response.write "</table>"
objRS.Close
Set objRS = Nothing
%>
<!-- We add a form to collect data and send it to addnew.asp -->
<br><form action="addnew.asp" method="post">
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td align="right">Name:</td>
<td align="left"><input type="Text" name="name"></td>
</tr><tr>
<td align="right">Last name:</td>
<td align="left"><input type="Text" name="lastname"></td>
</tr><tr>
<td align="right">Email:</td>
<td align="left"><input type="Text" name="email"></td>
</tr><tr>
<td align="right">Password:</td>
<td align="left"><input type="Text" name="password"></td>
</tr><tr>
<td align="right"></td>
<td align="left"><input type="Submit" value="Submit"></td>
</tr></table>
</form>
</BODY></HTML>
For now we won’t worry about data validation.
We’ll do that later as I do not want to make these first examples any more difficult.
Now, we’ll use the recordset method ADDNEW, which allows us to add a new record to any table in a database.
Here’s the code for addnew.asp
<% Option Explicit %>
<!-- #INCLUDE FILE="DBInfo.asp" -->
<%
Dim objRS
Set objRS = Server.CreateObject ("ADODB.Recordset")
objRS.Open "users", strConnect, adOpenStatic, adLockOptimistic, adCmdTable
‘ The previous line opens the DB with write permission.
objRS.AddNew ‘ This allows to update the table, then we provide the values.
objRS("name") = request.form("name")
objRS("lastname") = request.form("lastname")
objRS("email") = request.form("email")
objRS("password") = request.form("password")
objRS.Update ‘ This will commit and save the data to the DB
objRS.Close
Set objRS = Nothing
response.redirect "listing.asp"
%>
So, as you can see, it’s pretty easy to add new records to the Data Base using the AddNew and Update methods.
On Part III you’ll learn to modify existing records.