![]() |
|
|
Published: Friday, August 27, 1999 By Christopher Miller
This article is the second part in a 4 part series. 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:
Another feature of Hooray! will be the ability for visitors to add their site to the search engine. Hoping that your search engine will receive many submissions, we also want a way for administration to edit the sites added. Not only to delete unwanted sites from the search engine, but also to change contact or URL information if a registered user requests it. For these examples, we will be using the same database we used in Part 1 (companyx.mdb). Don't worry, in Part 4 we will be building Hooray! with the final database and putting everything together in a nice, neat package. Lets start be creating 3 pages; veiw.asp, edit.asp, and add.asp. The view.asp page is for the administrators, and lists every entry in the database. It is here that the administrator can select which action to take with each entry (edit or delete). The edit.asp page is a quick form that displays the contents of the selected record and allows the administrator to change any or all of the information. The add.asp page will contain a blank form that allows the visitors (and administrators) to add sites to the database. It is assumed that for both the veiw.asp and edit.asp pages you will want some kind of password protection, I will cover that topic in Part 4. OK, now for veiw.asp. We will be using the exact connection strings used in Part 1 for out Access Database connection, with one exception, we want to change it from a "read only" connection to a "Pessimistic" connection. We do this by simply changing one number in the code below: RecordSet.Open cmdDC, , 0, 1 gets changed to: RecordSet.Open cmdDC, , 0, 2 In this example, I will be showing you two different ways to capture the action to be used. The first one uses text links labeled "Edit" and "Delete". The second example uses command buttons. Both examples pass the record ID # through a QueryString. I do it this way to save on Session Variables. Text Links:
<table>
<%
If Not RecordSet.BOF Then
'-- Loop through records until we are at the end
Do Until RecordSet.EOF
'-- Display the fields
Response.Write("<tr>" & Chr(13))
'-- display the edit and delete links
Response.Write(" <td><a href='edit.asp?ID=" & RecordSet.Fields("ID") & "'>Edit</a> / <a href='view.asp?Action=Delete&ID=" & RecordSet.Fields("ID") & "'>Delete</a></td>" & Chr(13))
RecordSet.MoveNext You will notice that the action links are as follows:
<a href='edit.asp?ID=" & RecordSet.Fields("ID") & "'>Edit</a> Because it gets sent to the same page we are on, lets start with the delete link. We need to first see if the Action has been sent as "Delete", and then get the record ID from the QueryString:
If Request.QueryString("Action") = "Delete" Then
RecordToDelete = CInt(Request.QueryString("ID"))
'--Move to first record
If Not RecordSet.BOF Then
'--Find the record we want to delete
Do Until RecordSet.Fields("ID") = RecordToDelete
RecordSet.MoveNext
RecordSet.Delete You will notice that I use a Do...Loop to "scroll" through each record until I find the one that matched my ID. Once found, we simply delete it. Now onto Command buttons. very similar idea, but instead of checking if the Action in the QueryString is sent as "Delete", we simply check to see if the Delete button has been clicked. Lets start with displaying the table:
<form action="view.asp" method="post">
<table>
<td>ID</td>
</tr>
<%
'-- Go to the first record
If Not RecordSet.BOF Then
'-- Loop through records until we are at the end
Do Until RecordSet.EOF
'-- Display the fields
Response.Write("<tr>" & Chr(13))
RecordSet.MoveNext
</table>
<input type="submit" name="btnEdit" value="Edit">
</form>
If Request.Form("btnDelete") = "Delete" Then
RecordToDelete = CInt(Request.Form("rdoSelect"))
If Not RecordSet.BOF Then
Do Until RecordSet.Fields("ID") = RecordToDelete
RecordSet.MoveNext
Moving on to the edit section, we also grab the record ID from a QueryString in both the link and button examples. For the link example, we simply send the user to the edit.asp page and add on a QueryString called "ID". With the command button, we do the same thing, we just have to add an additional step:
If Request.Form("btnEdit") = "Edit" Then
If Request.QueryString("ID") <> "" Then
SQL = "SELECT tblEmployees.ID, tblEmployees.* FROM tblEmployees WHERE (((tblEmployees.ID)=" & Request.QueryString("ID") & "));"
End If
If the QueryString does not contain a value, you can either display the first record, or send the user back to the veiw.asp page. Now that we have the record we want to edit, lets display our table:
<table border="0" cellpadding="0" cellspacing="0">
If Request.Form("btnUpdate") = "Update" Then
RecordSet.Fields("Name") = Request.Form("txtName")
RecordSet.Update
End If
<% If Updated = "True" Then %>
<b><%= RecordSet.Fields("Name") %></b> has been updated.<P>
<% End If %>
RecordSet.AddNew We also do not need to grab a record ID frokm a QueryString because we are not editing a specific record. Here is the code for my entire add.asp page:
<%
'-- Declare your variables
Dim DataConnection, cmdDC, RecordSet
'-- Create object and open database
Set DataConnection = Server.CreateObject("ADODB.Connection")
Set cmdDC = Server.CreateObject("ADODB.Command")
'-- default SQL
SQL = "SELECT * FROM tblEmployees"
cmdDC.CommandText = SQL
'-- Cursor Type, Lock Type
'-- ForwardOnly 0 - ReadOnly 1
RecordSet.Open cmdDC, , 0, 2
If Request.Form("btnAdd") = "Add" Then
RecordSet.AddNew
RecordSet.Fields("Name") = Request.Form("txtName")
RecordSet.Update
End If
<form action="add.asp" method="post">
<% If Added = "True" Then %>
<b><%= Request.Form("txtName") %></b> has been Added.<P>
<% End If %>
<table border="0" cellpadding="0" cellspacing="0">
<p>
<input type="submit" name="btnAdd" value="Add">
</form>
<%
Set cmdDC = Nothing
That is all there really is to it! Be sure to keep your eyes open for Part 3, Creating a Customized homepage! Enjoy!
Attachments:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||