Creating a News management system (Part 6)
The following code will make up 'del_all.asp' which will show
all the news records.
<%@ Language="VBScript" %>
<% Option Explicit %>
<%
If Session("BlnLoggedIn") <>
True Then
Response.Redirect("login.asp")
End If
%>
<html>
<head><title>News Management System - Delete News</title>
</head>
<body>
<!--#include file="admin_navigation.asp"
-->
<%
Dim Connection, Recordset, sConnString
Dim SQL
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("../db/news.mdb")
Connection.Open(sConnString)
SQL ="SELECT * FROM tblNews"
Recordset.Open SQL , connection
If recordset.Eof Then
Response.write "<div align='center'>Sorry there are no current
records.</div>"
Else
Do while not recordset.Eof
response.write recordset("headline") & "<br>"
response.write recordset("news_story") & "<br>"
response.write recordset("story_date") & "<br>"
%>
<a href="delete_record.asp?id=<%= recordset("ID")
%>">delete record</a><br><br>
<%
recordset.movenext
Loop
End If
recordset.Close
Set recordset = Nothing
connection.Close
Set connection = Nothing
%>
</body>
</html>
Save 'del_all.asp' in the 'admin' folder. In the screenshot
above notice the news details and the hyperlink which is circled
in red for each news record. Attached to the end of this URL
we will be passing a value in the querystring
which is the unique ID of the news story. This was assigned
automatically to the record once it was entered into the database.
This helps us to uniquely identify that record. Once you click
on the hyperlink that ID is sent to the linked page. The linked
page will then receive and process the information and delete
the record with that ID. Simple!
Have a look at the code below for the 'delete_record.asp'
<%@ Language="VBScript" %>
<% Option Explicit %>
<%
If Session("BlnLoggedIn") <> True
Then
Response.Redirect("login.asp")
End If
%>
<html>
<head><title>News Management System - Delete Records</title>
</head>
<body>
<!--#include file="admin_navigation.asp" -->
<%
Dim Connection, sConnString
Dim SQL, ID
ID=request.querystring("ID")
Set Connection = Server.CreateObject("ADODB.Connection")
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("../db/news.mdb")
SQL="DELETE * FROM tblNews WHERE ID=" & ID
Connection.execute(SQL)
response.write "<div align='center'>The record was deleted.</div>"
connection.Close
Set connection = Nothing
%>
</body>
</html>
Save 'delete_record.asp' in the admin folder. Notice in the code above we don't use
the recordset object as we are not returning any records.
As well as being able to delete records learn how to
update records using the querystring.