Storing myLinks Information in the Database

The links table is pretty simple, so let's take a look at it first. We're going to need the ability to do three things: insert, update, and delete records.

We're going to start by creating a form that will list all of the current records, along with a check box to mark them for deletion. We'll also have a link to a form where we can edit the record and a link that will give us to a blank form we can use to create a new one.

Copy admintemplate.asp to edit_links.asp. Add the code in Listing 8.6.

Code Listing 8.6. edit_links.asp: Deciding which records to edit or delete
0: <%@ LANGUAGE="VBSCRIPT" %>
1: <!--#include file="adovbs.inc"-->
2: <!--#include file="pagetop.txt"-->
3: <!--#include file="isadmin.inc"-->
4: <%
5: if isAdmin then
6: %>
7: <CENTER>
8: <H2> Edit Links</h2>
9:
10:<A HREF="edit_links_rec.asp">Create a New Record</A>
11:
12:<FORM ACTION="edit_links_action.asp" METHOD="post">
13:
14:<TABLE BORDER="1">
15:<%
16:    set outpostDB = Server.CreateObject("ADODB.Connection")
17:    outpostDB.Open "outpost"
18:
19:    set tableSet = Server.CreateObject("ADODB.RecordSet")
20:    tableSet.Open "select * from links order by link_id",
21:        outpostDB, adOpenForwardOnly, adLockOptimistic, adCmdText
22:
23:    while not tableSet.EOF
24:%>      <TR>
25:            <TD>
26:               <INPUT TYPE="checkbox" NAME="p_delete"
27:                             VALUE="<%=tableSet("link_id")%>">

28:            </TD>
29:<%          p_numberOfColumns = tableSet.Fields.Count
30:%>          <TD>
31:               <A HREF="edit_links_rec.asp?p_link_id=<%
32:                Response.Write tableSet("link_id")%>">
33:                <%= tableSet.Fields(0).Value %>
34:             </A>
35:         </TD>
36:
37:<%       for col = 1 to (p_numberofColumns-1)
38:%>           <TD>
39:                <%= tableSet.fields(col).Value %>
40:            </TD>
41:<%       next %>
42:     </TR>
43:<%   tableSet.MoveNext
44:  wend
45:  tableSet.Close
46:  set tableSet = Nothing
47:%>
48:
49:</TABLE>
50:<INPUT TYPE="submit" VALUE="Delete Checked Rows">
51:</form>
52:
53:<A HREF="edit_links_rec.asp">Create a New Record</A>
54:
55:</CENTER>
56:<%
57:    outpostDB.Close
58:    set outpostDB = Nothing
59:else
60:
61:    Response.Write "You do not have access to this page."
62:
63:end if
64:%>
65:</BODY>
66:</HTML>

On line 10 we create a link to the new record form. We'll put it at the top and the bottom of the page, so it's less likely that our administrator will have to scroll for miles if there are a lot of records in our table. Obviously if there are too many records, we're going to have to manage this in a different way, but the basic idea is the same.

On line 12 we're creating a form that will note which records we want to delete. Displaying the table and the check boxes on lines 23 through 46 is very similar to what we did in display_table.asp. We could hard-code in the names of the fields, but if we change the table definition later, that'll come back to haunt us. The main difference between this and display_table.asp is that we've added a check box for every field on lines 26 and 27, and we've separated out the first column (number 0) on lines 30 through 35 so we can make it a link to an edit page (see Figure 8.2).

Let's look at that first column for a minute. It's link_id, the primary key for this table. That means that if we want to identify a specific record in the table, all we need is that value. We don't have to even know the actual link_name and link_desc. As long as we have the link_id, we can pinpoint which record it is. That's why it's important in your applications to make sure that every table has a primary key.

Figure 8.2. We can edit our tables directly from the Web.


Note

Even though we haven't been designating them as we've been building them, every table in our project has a primary key. Also, a primary key doesn't have to be just one column. For instance, in our order_items table, the primary key is actually a combination of the order_id and the prod_id.


Now we need a form action to take those check boxes and delete our records. Remember back in Chapter 4, "Database Access Using ASP," when we were comparing the links the user had chosen with the links that were in the table? We're doing the same thing here, only this time we're deleting the record when we find a match. When we're done deleting, we'll want to take the user back to the original form. Copy admintemplate.asp to edit_links_action.asp and add the code in Listing 8.7.

Code Listing 8.7. edit_links_action.asp: Deleting specific records
0: <%@ LANGUAGE="VBSCRIPT" %>
1: <%  Response.buffer = true  %>
2: <!--#include file="adovbs.inc"-->
3: <!--#include file="pagetop.txt"-->
4: <!--#include file="isadmin.inc"-->
5: <%
6: if isAdmin then
7:
8:     set outpostDB = Server.CreateObject("ADODB.Connection")
9:     outpostDB.Open "outpost"
10:
11:    set deleteSet = Server.CreateObject("ADODB.RecordSet")
12:    deleteSet.Open "select * from links order by link_id", _
13:        outpostDB, adOpenDynamic, adLockPessimistic, adCmdText
14:    p_thisDelete = 1
15:    while not deleteSet.EOF
16:        if cint(deleteSet("link_id")) = _
17:          cint(Request.form("p_delete").item(p_thisDelete)) then
18:            deleteSet.Delete
19:            deleteSetUpdate
20:            if p_thisDelete < Request.form("p_delete").Count then
21:                p_thisDelete = p_thisDelete + 1
22:            end if
23:        end if
24:        deleteSet.MoveNext
25:     wend
26:     deleteSet.Close
27:     set deleteSet = Nothing
28:
29:     outpostDB.Close
30:     set outpostDB = Nothing
31:
32:     Response.Clear
33:     Response.Redirect "http://localhost/edit_links.asp"
34:else
35:
36:     Response.Write "You do not have access to this page."
37:
38:end if
39:%>
40:</BODY>
41:</HTML>

In the past, when we were redirecting the user, we took great pains to make sure that we didn't send any text to the browser before we did it; we even went to the point of re-arranging the page. This time, instead of worrying about what we've already sent, we're going to tell the browser to hold all headers and text until the page is completely finished. To do this, on line 1 we tell the Response object to buffer it's output. Essentially, we're telling the Response object not to send anything at all until we tell it to, or until the page is completely finished. Then later, on line 32, just before we do the redirect, we tell it to throw away whatever else it was going to send and do the redirect instead.

Note

This behavior is the default in IIS 5.0, but in previous versions it was not, requiring the developer to set it.


Lines 11 through 27 are similar to what we did in Chapter 4, when we were managing the user's chosen links. We're creating a list of all links and then comparing them, one by one, against those that we've marked for deletion. When we find a record that has been checked off, we delete it and move on.

We could also have looped through the links for deletion and let the RecordSet find each one. Again, there's more than one way to solve this problem.

Now let's look at creating a new record. Copy admintemplate.asp to edit_links_rec.asp and add the code in Listing 8.8.

Code Listing 8.8. edit_links_rec.asp: Adding a record
0: <%@ LANGUAGE="VBSCRIPT" %>
1: <!--#include file="adovbs.inc"—>
2: <!--#include file="pagetop.txt"—>
3: <!--#include file="isadmin.inc"—>
4: <%
5: if isAdmin then
6:
7:     set outpostDB = Server.CreateObject("ADODB.Connection")
8:     outpostDB.Open "outpost"
9:
10:    set maxSet = outpostDB.Execute("select * from links "_
11:        & "order by link_id desc")
12:    linkSet.MoveFirst
13:    if maxSet.EOF then
14:       p_link_id = 1
15:    else
16:       p_link_id = maxSet("link_id") + 1
17:    end if
18:    maxSet.Close
19:    set maxSet = Nothing
20:%>
21:    <CENTER>
22:    <FORM ACTION="edit_links_rec_action.asp" METHOD="post">

23:    <h2>Add a New Link</h2>
24:    <TABLE>
25:    <TR>
26:        <TD>Link ID</TD>
27:        <TD>
28:           <INPUT TYPE="hidden" NAME="p_link_id"
29:               VALUE="<%= p_link_id %>">
30:           <%= p_link_id %>
31:       </TD>
32:   </TR>
33:   <TR>
34:       <TD>Link Name</TD>
35:       <TD><INPUT TYPE="text" NAME="p_name" SIZE="25">
36:   </TR>
37:   <TR>
38:       <TD>Link Description</TD>
39:       <TD><INPUT TYPE="text" NAME="p_desc" SIZE="50"
40:               MAXLENGTH="255">
41:   </TR>
42:   <TR>
43:     <TD>Link URL: http://</TD>
44:     <TD><INPUT TYPE="text" NAME="p_url" SIZE="50"
45:             MAXLENGTH="100">
46:  </TR>
47:
48:  </TABLE>
49:
50:  <INPUT TYPE="submit">
51:
52:  </form>
53:  </CENTER>
54:<%
55:  outpostDB.Close
56:  set outpostDB = Nothing
57:else
58:
59:  Response.Write "You do not have access to this page."
60:
61:end if
62:%>
63:</BODY>
64:</HTML>

What we have here is just a form that lets us add a link name, description, and URL. The only tricky part is getting a unique link_id. To do that, we're selecting the maximum existing link_id and adding 1 to it on lines 10 through 19.

Adding the record itself is simple. Copy admintemplate.asp to edit_links_rec_action.asp and add the code in Listing 8.9.

Code Listing 8.9. edit_links_rec_action.asp: Creating the new record
0: <%@ LANGUAGE="VBSCRIPT" %>
1: <!--#include file="adovbs.inc"-->
2: <!--#include file="pagetop.txt"-->
3: <!--#include file="isadmin.inc"-->
4: <%
5: if isAdmin then
6:
7:     p_link_id = cint(Request.form("p_link_id"))
8:     p_link_name = cstr(Request.form("p_name"))
9:   p_link_desc = cstr(Request.form("p_desc"))
10:  p_link_url = cstr(Request.form("p_url"))
11:
12:  set outpostDB = Server.CreateObject("ADODB.Connection")
13:  outpostDB.Open "outpost"
14:
15:  set insertSet = Server.CreateObject("ADODB.RecordSet")
16:  insertSet.Open "links", outpostdb, adOpenDynamic, _
17:    adLockOptimistic, adCmdTable
18:  insertSet.AddNew
19:  insertSet("link_id") = p_link_id
20:  insertSet("link_name") = p_link_name
21:  insertSet("link_desc") = p_link_desc
22:  insertSet("link_url") = p_link_url
23:  insertSetUpdate
24:  insertSet.Close
25:  set insertSet = Nothing
26:
27:  outpostDB.Close
28:  set outpostDB = Nothing
29:else
30:
31:  Response.Write "You do not have access to this page."
32:
33:end if
34:%>
35:</BODY>
36:</HTML>

On lines 7 through 10 we get the information from the form and convert it to the proper types because we're going to be putting it into the database. Then on lines 15 through 25 we create a RecordSet for the links table, add a new record, populate it, and close everything up. Nothing new there.

However, you may have noticed that when we created the edit_links.asp page, we were also sending the user to edit_links_rec.asp to change a record, as opposed to just adding a new one.

What we need is for the edit_links_rec.asp form to come up blank if we are creating a new record, but to be prepopulated if we're editing an existing one. Make the changes to edit_links_rec.asp shown in Listing 8.10.

Code Listing 8.10. edit_links_rec.asp: Editing an existing record
0: <%@ LANGUAGE="VBSCRIPT" %>
1: <!--#include file="adovbs.inc"-->
2: <!--#include file="pagetop.txt"-->
3: <!--#include file="isadmin.inc"-->
4: <%
5: if isAdmin then
6:
7:     set outpostDB = Server.CreateObject("ADODB.Connection")
8:     outpostDB.Open "outpost"
9:
10:    set linkSet = Server.CreateObject("ADODB.RecordSet")
11:    linkSet.Open "select * from links order by link_id desc", _
12:        outpostDB, adOpenStatic, adLockOptimistic, adCmdText
13:    linkSet.MoveFirst
14:    if linkSet.EOF then
15:       p_link_id = 1
16:    else
17:       p_link_id = linkSet("link_id") + 1
18:   end if
19:
20:   p_link_id_in = Request.querystring("p_link_id")

21:   if p_link_id_in <> "" then
22:       linkSet.Find "link_id = "& p_link_id_in
23:   end if
24:   if p_link_id_in = "" or linkSet.EOF then
25:       p_link_name = ""
26:       p_link_desc = ""
27:       p_link_url = ""
28:   else
29:       p_link_name = linkSet("link_name")
30:       p_link_desc = linkSet("link_desc")
31:       p_link_url = linkSet("link_url")
32:       p_link_id = p_link_id_in
33:       p_existing = p_link_id_in
34:    end if
35:
36:    linkSet.Close
36:    set linkSet = Nothing
37:%>
38:    <CENTER>
39:    <FORM ACTION="edit_links_rec_action.asp" METHOD="post">
40:    <INPUT TYPE="hidden" NAME="p_existing"
41:         VALUE="<%=p_existing%>">
42:    <h2>Edit Link</h2>
43:    <TABLE>
44:    <TR>
45:        <TD>Link ID</TD>
46:        <TD>
47:           <INPUT TYPE="hidden" NAME="p_link_id" VALUE="<%= p_link_id %>">
48:           <%= p_link_id %>
49:       </TD>
40:   </TR>
41:   <TR>
42:      <TD>Link Name</TD>
43:      <TD><INPUT TYPE="text" NAME="p_name" SIZE="25"
44               VALUE="<%=p_link_name%>">
45:   </TR>
46:   <TR>
47:      <TD>Link Description</TD>
48:      <TD><INPUT TYPE="text" NAME="p_desc" SIZE="50"
49               VALUE="<%=p_link_desc%>" MAXLENGTH="255">
50:   </TR>
51:   <TR>
52:      <TD>Link URL: http://</TD>
53:      <TD><INPUT TYPE="text" NAME="p_url" SIZE="50"
54:               VALUE="<%=p_link_url%>" MAXLENGTH="100">
55:   </TR>
56:
57:   </TABLE>
58:
59:   <INPUT TYPE="submit">
60:
61:   </form>
62:   </CENTER>
63:<%
64:   outpostDB.Close
65:   set outpostDB = Nothing
66:else
67:
68:   Response.Write "You do not have access to this page."
69: 

70:end if
71:%>
72:</BODY>
73:</HTML>

What we're doing on lines 20 through 34 is checking to see whether we're supposed to be editing an existing record, and if we are, we're extracting the information from the RecordSet we've already created and populating the form with it. If there is no existing record, the fields will come up blank, just like they did before.

Because we're going to be searching the RecordSet on lines 21 through 23, we've changed it from the default Forward-only cursor you get with an Execute statement to a Static cursor so we can move up the list as well as down.

On line 21, we're checking to see whether there was an existing record submitted. Because we're coming from a link instead of a form, we're using Request. querystring instead of Request.form. Just in case there's no value coming in, we'll call it p_link_id_in so we don't inadvertantly set p_link_id to an empty string.

If we did find a value for p_link_id_in, we're using it to find the appropriate record in the set. Then, whether we find it or not, we're populating variables that we're going to use later. Also, if we did find a value for p_link_id_in, we're using it to replace p_link_id. We'll also set a flag to tell us that we're dealing with an existing record. That way, when we get to the form action, we know whether to update a record or create a new one.

Finally we'll use the new (or blank) values to populate the text boxes.

Now we have a form that will handle both new and existing records. All that's left is to modify the form action to handle the existing records. Go back to edit_links_rec_action.asp and make the changes in Listing 8.11.

Code Listing 8.11. edit_links_rec_action.asp: Saving changes to an existing record
0: <%@ LANGUAGE="VBSCRIPT" %>
1: <%  Response.buffer = true%>
2: <!--#include file="adovbs.inc"-->
3: <!--#include file="pagetop.txt"-->
4: <!--#include file="isadmin.inc"-->
5: <%
6: if isAdmin then
7:
8:     p_link_id = cint(Request.form("p_link_id"))
9:     p_link_name = cstr(Request.form("p_name"))
10:    p_link_desc = cstr(Request.form("p_desc"))
11:    p_link_url = cstr(Request.form("p_url"))
12:    p_existing = cstr(Request.form("p_existing"))
13:
14:    set outpostDB = Server.CreateObject("ADODB.Connection")
15:    outpostDB.Open "outpost"
16:
17:    set insertSet = Server.CreateObject("ADODB.RecordSet")
18:  insertSet.Open "links", outpostdb, adOpenDynamic, _
19:      adLockOptimistic, adCmdTable
20:
21:  if p_existing = "" then
22:      insertSet.AddNew
23:  else
24:     insertSet.Find "link_id =" & p_existing
25:  end if
26:  insertSet("link_id") = p_link_id
27:  insertSet("link_name") = p_link_name
28:  insertSet("link_desc") = p_link_desc
29:  insertSet("link_url") = p_link_url
30:  insertSetUpdate
31:  insertSet.Close
32:  set insertSet = Nothing
33:
34:  outpostDB.Close
35:  set outpostDB = Nothing
36:
37:  Response.Clear
38:    Response.Redirect "http://localhost/edit_links.asp"
39:else
40:
41:    Response.Write "You do not have access to this page."
42:
43:end if
44:%>
45:</BODY>
46:</HTML>

All we had to do differently with this page was to check and see whether there was an existing record, and if there was, find it instead of creating a new one. That's it! It certainly beats deciding whether to create an insert statement or an update statement and having to maintain them both!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.130.232