By Christopher Miller
This article is the third part in a 4 part series (read Part 1 | Part 2) . The series will be a complete tutorial of creating your own search engine and web portal called - Hooray! I will be discussing the following techniques:
- Sorting and Searching Database Tables
- Adding, Deleting, and Updating Records in a Database
- Creating a Customized homepage (using a database to store preferences)
- Putting It All Together (combines all previous articles as well as others to create Hooray!)
Some of you may have read my article on creating a customized homepage. The article discusses how to create a homepage using cookies to store the user preferences. This article is along the same lines (and even uses the same look and feel) but with one exception, we will be using a database to store the user preferences. The only cookie we need is for storage of the unique user ID that we will be using to display the custom page. This will allow us to eliminate the need for a login screen.
This method is much easier to manage and will produce a much better end result. So let's get started...
We will be using 7 ASP pages in all:
- global.asa
- default.asp
- register.asp
- my_hooray.asp
- edit_globals.asp
- edit_links.asp
- edit_emails.asp
| tblUsers: | |
| uID | AutoNumber |
| uName | Text |
| uEmail | Text |
| tblFormatting: | |
| fID | AutoNumber |
| fTextColor | Text |
| FLinkColor | Text |
| fVLinkColor | Text |
| fALinkColor | Text |
| fBGColor | Text |
| fUserID | Text |
| fTableColor | Text |
| tblLinks: | |
| lID | AutoNumber |
| lUserID | Number |
| lLInk | Text |
| lName | Text |
| tblAddressBook: | |
| aID | AutoNumber |
| aUserID | Number |
| aEmail | Text |
| aName | Text |
Let's start with the global.asa file. I am simply using this to store the path of the database. This allows us to move the database to a different location without much modification.
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
SUB Session_OnStart
Session("DatabasePath") = "C:\databases\"
END SUB
</SCRIPT>
Next, the default.asp page simply checks for the existance of a cookie on the client’s machine. If there is no cookie stored, we send the user to a registration page (register.asp).
<%
If Request.Cookies("UserID") = "" Then
'-- check for User Id
Response.Redirect("register.asp")
Else
Session("UserID") = Request.Cookies("UserID")
Response.Redirect("my_hooray.asp")
End If
%>
<%
Dim DataConnection, cmdDC, RecordSet, SQL, varDatabaseName
varDatabaseName = "hooray.mdb"
<!--#include file="common/data_conn_open.asp"-->
<%
RecordSet.Fields("uName") = Request.Form("txtName")
RecordSet.Update
'-- get New User ID
If Not RecordSet.BOF Then
Do Until RecordSet.EOF
If RecordSet.Fields("uName") = Request.Form("txtName") Then
Session("UserID") = RecordSet.Fields("uID")
End If
RecordSet.MoveNext
<!--#include file="common/data_conn_close.asp"-->
If Request.Form("btnRegister") = "Register" Then
SQL = "SELECT * FROM tblUsers;"
%>
RecordSet.AddNew
RecordSet.Fields("uEmail") = Request.Form("txtEmail")
RecordSet.ReQuery
RecordSet.MoveFirst
End If
If RecordSet.Fields("uEmail") = Request.Form("txtEmail") Then
End If
Loop
%>
Once we have the new user in the database, and we have their unique ID stored in a session variable, we can add them to the formatting database. I have set the default values right in the database rather than on the homepage itself to make the code a lot cleaner and easier to manage.
<%
SQL = "SELECT tblFormatting.fUserID FROM tblFormatting;"
<!--#include file="common/data_conn_open.asp"-->
<%
RecordSet.Fields("fUserID") = Session("UserID")
RecordSet.Update
<!--#include file="common/data_conn_close.asp"-->
<%
Response.Cookies("UserID") = Session("UserID")
'-- Send the user to their homepage when done
Response.Redirect("default.asp")
End If
'-- Add new user to the formatting database
%>
RecordSet.AddNew
%>
'-- Set the user id cookie
Response.Cookies("UserID").Expires = Date() + 365
%>
<%
varDatabaseName = "hooray.mdb"
'-- Default SQL to get name and formatting
SQL = "SELECT tblUsers.uName, tblFormatting.*, tblUsers.uID, tblUsers.uEmail "
<!--#include file="common/data_conn_open.asp"-->
<%
varUserEmail = RecordSet.Fields("uEmail")
End If
<!--#include file="common/data_conn_close.asp"-->
Dim DataConnection, cmdDC, RecordSet, SQL, varDatabaseName
Dim varUserName, varUserEmail
Dim varfTextColor, varfLinkColor, varfvLinkColor
Dim varfALinkColor, varfBGColor, varfTableColor
SQL = SQL & "FROM tblFormatting INNER JOIN tblUsers ON tblFormatting.fUserID = tblUsers.uID "
SQL = SQL & "WHERE (((tblUsers.uID)=" & Session("UserID") & "));"
%>
If Not RecordSet.BOF Then
RecordSet.MoveFirst
varUserName = RecordSet.Fields("uName")
varfTextColor = RecordSet.Fields("fTextColor")
varfLinkColor = RecordSet.Fields("fLinkColor")
varfVLinkColor = RecordSet.Fields("fvLinkColor")
varfALinkColor = RecordSet.Fields("fALinkColor")
varfBGColor = RecordSet.Fields("fBGColor")
varfTableColor = RecordSet.Fields("fTableColor")
%>
<STYLE TYPE="text/css">
<--
BODY {
color : <%= varfTextColor %>;
background-color : <%= varfBGColor %>;
}
A:Active {
color : <%= varfALinkColor %>;
}
A:Visited {
color : <%= varfVLinkColor %>;
}
A:Link {
color : <%= varfLinkColor %>;
}
-->
</STYLE>
<%
SQL = "SELECT tblLinks.lLink, tblLinks.lName "
<!--#include file="common/data_conn_open.asp"-->
<%
<a href="<%= RecordSet.Fields("lLink") %>"><%= RecordSet.Fields("lName") %></a><br>
<%
If RecordSet.recordCount = 0 Then
<!--#include file="common/data_conn_close.asp"-->
<%
SQL = "SELECT tblAddressBook.aEmail, tblAddressBook.aName "
<!--#include file="common/data_conn_open.asp"-->
<%
<option value="<%= RecordSet.Fields("aEmail") %>"><%= RecordSet.Fields("aName") %></option>
<%
<!--#include file="common/data_conn_close.asp"-->
'--Insert links
SQL = SQL & "FROM tblLinks "
SQL = SQL & "WHERE (((tblLinks.lUserID)=" & Session("UserID") & ")) "
SQL = SQL & "ORDER BY tblLinks.lName;"
%>
If Not RecordSet.BOF Then
RecordSet.MoveFirst
Do Until RecordSet.EOF
%>
RecordSet.MoveNext
Loop
End If
Response.Write("<i>You have no links saved...</i>")
End If
%>
'--Insert Email addresses
SQL = SQL & "FROM tblAddressBook "
SQL = SQL & "WHERE (((tblAddressBook.aUserID)=" & Session("UserID") & ")) "
SQL = SQL & "ORDER BY tblAddressBook.aName;"
%>
If Not RecordSet.BOF Then
RecordSet.MoveFirst
Do Until RecordSet.EOF
%>
RecordSet.MoveNext
Loop
End If
%>
Let's move onto edit_globals.asp, edit_emails.asp, and edit_links.asp. All three pages do essentially the same thing. I use a SQL statement to grab only the records that belong to a particular user, and then display and save new values. I am simply going to list all 3 pages below (including the HTML).
edit_globals.asp
<%
'-- Default SQL to get name and formatting
SQL = "SELECT tblUsers.uName, tblFormatting.*, tblUsers.uID, tblUsers.uEmail "
<!--#include file="common/data_conn_open.asp"-->
<%
If Request.Form("btnSave") = "Save" Then
If Not RecordSet.BOF Then
RecordSet.Fields("uEmail") = Request.Form("txtEmail")
RecordSet.Update
End If
'-- Get values
If Not RecordSet.BOF Then
varUserEmail = RecordSet.Fields("uEmail")
End If
<!--#include file="common/data_conn_close.asp"-->
<html>
<center>
<font size="4">Editing Globals for <%= varUserName %></font><P>
<form action="edit_globals.asp" method="post">
<table border="0" cellpadding="0" cellspacing="0">
</form>
<p>
<a href="my_hooray.asp">Back Home</a>
</center>
</body>
Dim DataConnection, cmdDC, RecordSet, SQL, varDatabaseName
Dim varUserName, varUserEmail
Dim varfTextColor, varfLinkColor, varfvLinkColor
Dim varfALinkColor, varfBGColor, varfTableColor
varDatabaseName = "hooray.mdb"
SQL = SQL & "FROM tblFormatting INNER JOIN tblUsers ON tblFormatting.fUserID = tblUsers.uID "
SQL = SQL & "WHERE (((tblUsers.uID)=" & Session("UserID") & "));"
%>
'-- Insert New values
RecordSet.MoveFirst
RecordSet.Fields("uName") = Request.Form("txtName")
RecordSet.Fields("fTextColor") = Request.Form("txtTextColor")
RecordSet.Fields("fLinkColor") = Request.Form("txtLColor")
RecordSet.Fields("fVLinkColor") = Request.Form("txtVColor")
RecordSet.Fields("fALinkColor") = Request.Form("txtAColor")
RecordSet.Fields("fBGColor") = Request.Form("txtBGColor")
RecordSet.Fields("fTableColor") = Request.Form("txtTableColor")
RecordSet.ReQuery
End If
RecordSet.MoveFirst
varUserName = RecordSet.Fields("uName")
varfTextColor = RecordSet.Fields("fTextColor")
varfLinkColor = RecordSet.Fields("fLinkColor")
varfVLinkColor = RecordSet.Fields("fvLinkColor")
varfALinkColor = RecordSet.Fields("fALinkColor")
varfBGColor = RecordSet.Fields("fBGColor")
varfTableColor = RecordSet.Fields("fTableColor")
%>
<body>
<tr>
<td align="right">Name:</td>
<td><input type="text" name="txtName" value="<%= varUserName %>" size="20"></td>
</tr>
<tr>
<td align="right">Email:</td>
<td><input type="text" name="txtEmail" value="<%= varUserEmail %>" size="20"></td>
</tr>
<tr>
<td align="right">Text Color:</td>
<td><input type="text" name="txtTextColor" value="<%= varfTextColor %>" size="20"></td>
</tr>
<tr>
<td align="right">Background Color:</td>
<td><input type="text" name="txtBGColor" value="<%= varfBGColor %>" size="20"></td>
</tr>
<tr>
<td align="right">Table Color:</td>
<td><input type="text" name="txtTableColor" value="<%= varfTableColor %>" size="20"></td>
</tr>
<tr>
<td align="right">Link Color:</td>
<td><input type="text" name="txtLColor" value="<%= varfLinkColor %>" size="20"></td>
</tr>
<tr>
<td align="right">Visited Link Color:</td>
<td><input type="text" name="txtVColor" value="<%= varfVLinkColor %>" size="20"></td>
</tr>
<tr>
<td align="right">Active Link Color:</td>
<td><input type="text" name="txtAColor" value="<%= varfALinkColor %>" size="20"></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btnSave" value="Save"></td>
</tr>
</table>
</html>
<%
varDatabaseName = "hooray.mdb"
SQL = "SELECT tblAddressBook.*, tblAddressBook.aUserID "
<!--#include file="common/data_conn_open.asp"-->
<%
Dim RecordToDelete
RecordToDelete = CInt(Request.QueryString("ID"))
If Not RecordSet.BOF Then
Do Until RecordSet.Fields("aID") = RecordToDelete
RecordSet.MoveNext
RecordSet.Delete
End If
If Request.Form("btnSave") = "Save Email" Then
RecordSet.AddNew
RecordSet.Fields("aUserID") = Session("UserID")
RecordSet.Update
End If
<html>
<table border="1" cellpadding="3" cellspacing="0" width="100%">
<%
Do Until RecordSet.EOF
<tr>
<%
Loop
</table>
<!--#include file="common/data_conn_close.asp"-->
<p align="center">
<form action="edit_emails.asp" method="post">
Name: <input type="text" name="txtName" size="30">
</form>
<p align="center">
<a href="my_hooray.asp">Back Home</a>
</body>
Dim DataConnection, cmdDC, RecordSet, SQL, varDatabaseName
SQL = SQL & "FROM tblAddressBook "
SQL = SQL & "WHERE (((tblAddressBook.aUserID)=" & Session("UserID") & "));"
%>
If Request.QueryString("Action") = "Delete" Then
RecordSet.MoveFirst
End If
Loop
RecordSet.Requery
RecordSet.Fields("aName") = Request.Form("txtName")
RecordSet.Fields("aEmail") = Request.Form("txtEmail")
RecordSet.Requery
%>
<body>
<tr>
<td align="center"><b>Action</b></td>
<td align="center"><b>Name</b></td>
<td align="center"><b>Eamil</b></td>
</tr>
If Not RecordSet.BOF Then
RecordSet.MoveFirst
End If
%>
<td><a href="edit_emails.asp?Action=Delete&ID=<%= RecordSet.Fields("aID") %>">Delete</a></td>
<td><%= RecordSet.Fields("aName") %></td>
<td><%= RecordSet.Fields("aEmail") %></td>
</tr>
RecordSet.MoveNext
%>
Email: <input type="text" name="txtEmail" size="30">
<input type="submit" name="btnSave" value="Save Email">
</html>
<%
varDatabaseName = "hooray.mdb"
SQL = "SELECT tblLinks.*, tblLinks.lUserID "
<!--#include file="common/data_conn_open.asp"-->
<%
Dim RecordToDelete
RecordToDelete = Int(Request.QueryString("ID"))
If Not RecordSet.BOF Then
Do Until RecordSet.Fields("lID") = RecordToDelete
RecordSet.MoveNext
RecordSet.Delete
End If
If Request.Form("btnSave") = "Save Link" Then
RecordSet.AddNew
RecordSet.Fields("lUserID") = Session("UserID")
RecordSet.Update
End If
<html>
<table border="1" cellpadding="3" cellspacing="0" width="100%">
<%
Do Until RecordSet.EOF
<tr>
<%
Loop
</table>
<!--#include file="common/data_conn_close.asp"-->
<p align="center">
<form action="edit_links.asp" method="post">
Name: <input type="text" name="txtName" size="30">
</form>
<p align="center">
<a href="my_hooray.asp">Back Home</a>
</body>
Dim DataConnection, cmdDC, RecordSet, SQL, varDatabaseName
SQL = SQL & "FROM tblLinks "
SQL = SQL & "WHERE (((tblLinks.lUserID)=" & Session("UserID") & "));"
%>
If Request.QueryString("Action") = "Delete" Then
RecordSet.MoveFirst
End If
Loop
RecordSet.Fields("lName") = Request.Form("txtName")
RecordSet.Fields("lLink") = Request.Form("txtURL")
RecordSet.Requery
%>
<body>
<tr>
<td align="center"><b>Action</b></td>
<td align="center"><b>Name</b></td>
<td align="center"><b>URL</b></td>
</tr>
If Not RecordSet.BOF Then
RecordSet.MoveFirst
End If
%>
<td><a href="edit_links.asp?Action=Delete&ID=<%= RecordSet.Fields("lID") %>">Delete</a></td>
<td><%= RecordSet.Fields("lName") %></td>
<td><%= RecordSet.Fields("lLink") %></td>
</tr>
RecordSet.MoveNext
%>
URL: <input type="text" name="txtURL" size="30">
<input type="submit" name="btnSave" value="Save Link">
</html>
<%
For Each Item In Request.Cookies
varDatabaseName = "hooray.mdb"
SQL = "SELECT tblUsers.uID, tblUsers.* "
<!--#include file="common/data_conn_open.asp"-->
<% RecordSet.Delete %>
<!--#include file="common/data_conn_close.asp"-->
<%
Response.Redirect("default.asp")
Dim Item
Dim DataConnection, cmdDC, RecordSet, SQL
Response.Cookies(Item) = ""
Next
SQL = SQL & "FROM tblUsers "
SQL = SQL & "WHERE (((tblUsers.uID)=" & Session("UserID") & "));"
%>
Session.Abandon
%>
Keep your eyes open for my next arrticle that will put this (and all of my other articles) into one giant application called Hooray!. Enjoy!
Related Articles
Attachments:



