ADO Queries and Working with Recordsets - How To Delete a Record
(Page 5 of 5 )
The first step in allowing a user to delete a record is to display our table of data, like so:
<html>
<body>
<%
set conn=Server.CreateObject(“ADODB.Connection”)
conn.Provider=”Microsoft.Jet.OLEDB.4.0”
conn.Open “c:/website/chucknorrisvictims.mdb”
set rs=Server.CreateObject(“ADODB.Recordset”)
rs.Open “SELECT * FROM Victims”,conn
%>
<table border=”1” width=”100%”>
<tr>
<%
for each x in rs.Fields
response.write(“<th>” & ucase(x.name) & “</th>”)
next
%>
</tr>
<% do until rs.EOF %>
<tr>
<form method=”post” action=”sampledelete.asp”>
<%
for each x in rs.Fields
if x.name=”VictimName” then%>
<td>
<input type=”submit” name=”VictimName” value=”<%=x.value%>”>
</td>
<%else%>
<td><%Response.Write(x.value)%></td>
<%end if
next
%>
</form>
<%rs.MoveNext%>
</tr>
<%
loop
conn.close
%>
</table>
</body>
</html>
The above code displays the data in our database. If the user clicks on any of the Victim Names, they will be taken to an ASP page where they can opt to delete the record. Here is the code for that page:
<html>
<body>
<%
set Conn=Server.CreateObject(“ADODB.Connection”)
conn.Provider=”Microsoft.Jet.OLEDB.4.0”
conn.Open “c:/website/chucknorrisvictims.mdb”
vic=Request.Form(“VictimName”)
if Request.Form(“VictimName”)=”” then
set rs.Server.CreateObject(“ADODB.Recordset”)
rs.open “SELECT * FROM Victims WHERE VictimName='” & vic & “'”,conn
%>
<form method=”post” action=”sampledelete.asp”>
<table>
<%for each x in rs.Fields%>
<tr>
<td><%=x.name%>” value=”<%=x.value%>”></td>
<%next>
</tr>
</table>
<br /><br /><br />
<input type=”submit” value=”Click to Delete”>
</form>
<%
else
sql=”DELETE FROM Victims”
sql=sql & “WHERE VictimName='” & vic & “'”
on error resume next
conn.Execute sql
if err<>0 then
response.write(“You do not have permission to delete”>
else
response.write(“The Record “ & vic & “ has been deleted.”)
end if
end if
conn.close
%>
</body>
</html>
This program will display the record the user chose from the previous page, and give them the option to delete the record. If they do not have permission, they will receive an error message; otherwise the record will be deleted and they will receive a message saying the record has been deleted.
Well that's it for this episode. In the next article we will learn how to update a database record, and start learning about ADO Objects.
Till then...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |