PART III : Modifying Records
We’ll need to do some changes in listing.asp, create edit.asp and create saveEdit.asp
First we'll start by adding a column to our table in listing.asp
Here’s just the new code for that table, replace this in listing.asp:
<%
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><td><b>Edit</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><td><a href='edit.asp?id="&objRS("id")&"'>Edit</a></td></tr>"
objRS.MoveNext
Wend
response.write "</table>"
objRS.Close
Set objRS = Nothing
%>
We’ll use the ID to pass it as a parameter in the link, that way; edit.asp will know what record will be modified.
Here’s the code for edit.asp
<% Option Explicit %>
<!-- #INCLUDE FILE="DBInfo.asp" -->
<%
Dim objRS, strQuery
Set objRS = Server.CreateObject ("ADODB.Recordset")
strQuery= "select * from USERS where id="&request.querystring("id")
objRS.Open strQuery, strConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
%>
<html><head><title>Editing existing records</title></head>
<body>Editing record: <%= objRS("id") %>
<br><form action="saveEdit.asp" method="post">
<table cellspacing="2" cellpadding="2" border="0">
<tr><td align="right">ID:</td>
<td align="left"><%= objRS("id") %><input type="hidden" name="id" value="<%= objRS("id") %>"></td></tr>
<tr><td align="right">Name:</td>
<td align="left"><input type="Text" name="name" value="<%= objRS("name") %>"></td></tr>
<tr><td align="right">Last name:</td>
<td align="left"><input type="Text" name="lastname" value="<%= objRS("lastname") %>"></td></tr>
<tr><td align="right">Email:</td>
<td align="left"><input type="Text" name="email" value="<%= objRS("email") %>"></td></tr>
<tr><td align="right">Password:</td>
<td align="left"><input type="Text" name="password" value="<%= objRS("password") %>"></td></tr>
<tr><td align="right"></td>
<td align="left"><input type="Submit" value="Save"></td>
</tr></table></form>
</body></html>
With the previous code, we displayed an HTML Form filled with the data retrieved from the DB.
This will allow showing what is currently stored and the user can modify only what he/she needs.
Now, for the final part, save the following code to saveEdit.asp
<% Option Explicit %>
<!-- #INCLUDE FILE="DBInfo.asp" -->
<%
Dim objRS, id,strQuery
Set objRS = Server.CreateObject ("ADODB.Recordset")
strQuery= "select * from USERS where id="&request.form("id")
objRS.Open strQuery, strConnect, adOpenStatic, adLockOptimistic, adCmdText
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"
%>
With these 3 steps, we can edit any existing record in our database and save those changes.
In the following page, we’ll learn to delete existing records.