access|asp+| Tutorial
On the web to introduce how to write a forum for many articles, but I observed that most of the code has varying degrees of error, will mislead everyone. So the purpose of my writing this article is to provide you with the code and ideas that have been successfully tested by me. Let's start with the establishment of the database:
To create a database with access:
First we want to build a database that holds posts, and here I introduce the method of using access2000 to build a database.
Let's start with a database file called News.mdb, and then click "New" in it to create two tables, one named details to hold the reply, and the other title to hold the theme as shown:
The production process is detailed:
Create the following fields in titles, as shown in the figure:
Attention:
1 |
Right-click "TitleID" and select "PRIMARY Key" to show the key graphic. |
2 |
Here the "Shu" can not be changed to "number" or there will be errors, Wangguojong of the "ASP & Web Database" made such a mistake. |
3 |
Set the default value for the field of the date/time type to now () |
Create the following fields in titles, as shown in the figure:
Set the default value for the field of the date/time type to now (). When the build is complete, you want to set the association of the two tables. Click the button and the following dialog box appears:
Drag the TitleID in the titles to the TitleID on the details, and the following dialog box appears: (as shown in diagram settings)
Then you can save it. Such a database for storage is established. Next we are going to write an ASP program. First, let's analyze the functionality that the forum needs. Some of the following features are probably required:
1 |
Show topics |
2 |
Publishing topics |
3 |
Show reply |
4 |
Post a reply |
So, based on these features, we need to write 6 ASP files:
title.asp, titlenew.asp, titleout.asp, detail.asp, detnew.asp, detout.asp
There is also a file that is required (in my program) Adovbs.inc, which provides constant values such as "adOpenStatic" in the program. ( click here adovbs.inc download source file ) code for each file:
Title.asp:
<% @LANGUAGE = "VBSCRIPT" codepage= "CP_ACP"%> <!--#include virtual= "Adovbs.inc"--> <!--#include file= "titleout.asp"--> <% Const head= "My Forum" Dbpath=server. MapPath ("News.mdb") Set Conn=server. CreateObject ("Adodb.connection") Conn.Open "Driver={microsoft Access driver (*.mdb)};d bq=" &dbpath Sql= "Select Titleid,createdate as Theme release time, lastnewsdate as last reply time, name as author, Shu as reply number, subject as topic from Lastnewsdate DESC " Set Rs=server. CreateObject ("Adodb.recordset") Rs.Open sql,conn,adopenstatic %> <title><%=head%></title> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <style type= "Text/css" > <!-- Body {font-family: "XXFarEastFont-Arial"; font-size:12px} Table {font-family: "XXFarEastFont-Arial"; font-size:12px} a:link {font-family: "Song body"; font-size:12px color: #000000; Text-decoration:none} a:visited {font-family: "Song body"; font-size:12px color: #666666; Text-decoration:none} a:hover {font-family: "Song body"; font-size:12px color: #ff3300; Text-decoration:underline} --> </style> <body> <!--output Theme--> <% rs.pagesize=10 PAGE=CLNG (Request ("page") If Page<1 then page=1 If Page>rs.pagecount then Page=rs.pagecount %> <form action= "title.asp" method= "Get" > <table border= "0" cellpadding= "0" cellspacing= "0" width= "90%" > <tr> <TD align= "Right" width= "100%" > <% If Page<>1 Then Response. Write ("<a href=title.asp?page=1> first page </a>") Response. Write ("<a href=title.asp?page=" & (page-1) & "> Prev </a>") End If If Page<>rs.pagecount Then Response. Write ("<a href=title.asp?page=" & (page+1) & "> Next page </a>") Response. Write ("<a href=title.asp?page=" &rs.pagecount& "> Last page </a>") End If %> Go to <input type= "text" name= "page" size= "3" > page Page: <font color= "#FF0000" ><%=page%></font>/<font color= "#FF0000" ><%=rs.pagecount%> </font> </td> </tr> </table> </form> <center> <table border= "0" bgcolor= "#999999" cellpadding= "3" cellspacing= "1" width= "780" > <tr bgcolor= "#00FFFF" > <TD height= "width=" align= "center" > Theme Release Time </td> <TD height= "width=" align= "center" > Theme </td> <TD height= "width=" align= "center" > Author </td> <TD height= "width=" align= "center" > Reply </td> <TD height= "width=" align= "Center" > Final reply Time </td> </tr> <% On Error Resume Next Rs.absolutepage=page For I=1 to Rs.pagesize Titleoutput RS Rs.movenext If Rs.eof then exit for Next %> </table> </center> <form action= "title.asp" method= "Get" > <table border= "0" cellpadding= "0" cellspacing= "0" width= "90%" > <tr> <TD align= "Right" width= "100%" > <% If Page<>1 Then Response. Write ("<a href=title.asp?page=1> first page </a>") Response. Write ("<a href=title.asp?page=" & (page-1) & "> Prev </a>") End If If Page<>rs.pagecount Then Response. Write ("<a href=title.asp?page=" & (page+1) & "> Next page </a>") Response. Write ("<a href=title.asp?page=" &rs.pagecount& "> Last page </a>") End If %> Go to <input type= "text" name= "page" size= "3" > page Page: <font color= "#FF0000" ><%=page%></font>/<font color= "#FF0000" ><%=rs.pagecount%> </font> </td> </tr> </table> </form> <!--below for input form--> <form action= "titlenew.asp" method= "POST" > <center> <table border= "0" > <tr> <td> name:</td> <td><input type= "hidden" size= "name=" name "value=" <%=session ("name")%> "><%=session" ("Name" )%></td> </tr> <tr> <td> Mail:</td> <td><input type= "text" size= "name=" email "value=" <%=session ("email")%> "></td> </tr> <tr> <td> Theme:</td> <td><input type= "text" size= "name=" "Subject" ></td> </tr> <tr> <td> content:</td> <td><textarea name= "Words" rows= "8" cols= "></textarea></td>" </tr> <tr> <TD align= "center" colspan= "2" ><input type= "Submit" value= "submitted" > <input type= "reset" value= "Empty" ></td> </tr> </table> </center> </form> </body>
|
Titlenew.asp:
<% @LANGUAGE = "VBSCRIPT" codepage= "CP_ACP"%> <!--#include virtual= "Adovbs.inc"--> <% Dbpath=server. MapPath ("News.mdb") Set Conn=server. CreateObject ("Adodb.connection") Conn.Open "Driver={microsoft Access driver (*.mdb)};d bq=" &dbpath Name=request ("name") Email=request ("Email") Subject=request ("Subject") Words=request ("words") If subject= "" or words= "" or name= "" or email= "" Then outputmsg= field cannot be empty, please fill in the full information! " Else Set Rs=server. CreateObject ("Adodb.recordset") Rs.Open "Titles", Conn,adopendynamic,adlockpessimistic Rs.addnew RS ("name") =name RS ("Email") =email RS ("subject") =subject RS ("words") =words RS ("Shu") =0 Rs.update Outputmsg= "Your theme has been added!" " Session ("name") =name Session ("Email") =email End If %> <title>untitled document</title> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <meta http-equiv= "Refresh" content= "3; Url=title.asp "> <body> <%=outputmsg%> </body> |
Titleout.asp:
<% Sub Titleoutput (RS) %> <tr bgcolor= "#ffffff" > <TD valign= "Top" height= "><%=rs" ("topic Publishing Time")%></td> <TD valign= "Top" height= "><a href=" Detail.asp?titleid=<%=rs ("TitleID")%> "><%=rs" ("subject")% ></a></td> <TD valign= "Top" height= "a" ><%=rs ("author")%></td> <TD valign= "Top" align= "right" height= "><%=rs" ("Reply number")%></td> <TD valign= "Top" height= "><%=rs" ("Last reply Time")%></td> </tr> <% End Sub %> |
Detail.asp
<% @LANGUAGE = "VBSCRIPT" codepage= "CP_ACP"%> <!--#include file= "detout.asp"--> <% If not session ("passed") then Response. Redirect ("title.asp") End If %> <% Dbpath=server. MapPath ("News.mdb") Set Conn=server. CreateObject ("Adodb.connection") Conn.Open "Driver={microsoft Access driver (*.mdb)};d bq=" &dbpath Titleid=request ("TitleID") Set Rs=conn.execute ("SELECT * FROM titles where titleid=" &clng (TitleID)) If Rs.eof Then Response. Redirect ("title.asp") Else Sql= "SELECT * from details where titleid=" &CLNG (TitleID) & "ORDER BY detailid Desc" Set Rsdetail=conn.execute ("SELECT * from details where titleid=" &CLNG (TitleID) & ' ORDER by Detailid ') End If %> <title>untitled document</title> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <body> <% Words=replace ("" &rs ("words"), Chr (+), "<br>") Email= "<a href=" "&rs (" Email ") &"mailto: "&rs (" email ") &" ">" &rs ("email") & "</a>" %> <center> <table> <tr> <td> Author: <%=rs ("name")%></td> <td>Email:<%=Email%></td> <td> Time: <%=rs ("CreateDate")%></td> </tr> <tr> <TD colspan= "3" > Theme: <%=rs ("subject")%></td> </tr> <tr> <TD colspan= "3" ><%=words%></td> </tr> </table> </center> <% While not rsdetail.eof Detailoutput rsdetail Rsdetail.movenext Wend %> <center> Post reply </center> <form action= "detnew.asp" method= "POST" > <input type= "hidden" name= TitleID "value=" <%=request ("TitleID")%> "> <center> <table border= "0" > <tr> <td> name:</td> <td><input type= "text" size= "name=" name "value=" <%=session ("name")%> "></td> </tr> <tr> <td> Mail:</td> <td><input type= "text" size= "name=" email "value=" <%=session ("email")%> "></td> </tr> <tr> <td> Theme:</td> <td><input type= "text" size= "name=" "Subject" ></td> </tr> <tr> <td> content:</td> <td><textarea name= "Words" rows= "8" cols= "></textarea></td>" </tr> <tr> <TD align= "center" colspan= "2" ><input type= "Submit" value= "submitted" > <input type= "reset" value= "Empty" ></td> </tr> </table> </center> </form> <center><a href= "title.asp" > Return to Theme Page </a></center> </body> |
Detnew.asp
<% @LANGUAGE = "VBSCRIPT" codepage= "CP_ACP"%> <!--#include virtual= "Adovbs.inc"--> <% Dbpath=server. MapPath ("News.mdb") Set Conn=server. CreateObject ("Adodb.connection") Conn.Open "Driver={microsoft Access driver (*.mdb)};d bq=" &dbpath Titleid=request ("TitleID") Name=request ("name") Email=request ("Email") Subject=request ("Subject") Words=request ("words") If subject= "" or words= "" or name= "" or email= "" Then outputmsg= field cannot be empty, please fill in the full information! " Else Set Rs=server. CreateObject ("Adodb.recordset") Rs.Open "Details", conn,adopendynamic,adlockpessimistic Rs.addnew RS ("name") =name RS ("Email") =email RS ("subject") =subject RS ("words") =words RS ("TitleID") =titleid Rs.update Outputmsg= "Your reply has been added!" " Session ("name") =name Session ("Email") =email Sql= "Update titles set Lastnewsdate=now (), shu=shu+1 where titleid=" &CLNG (TitleID) Conn.execute (SQL) End If %> <title>untitled document</title> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <meta http-equiv= "Refresh" content= "1; Url=detail.asp?titleid=<%=titleid%> "> <body> <center><%=outputmsg%></center> </body> |
Detout.asp
Words=replace (words, "", " ") Email= "<a" Href= "" "&rs (" email ") &" mailto: "&rs (" email ") &" "" > "&rs (" email ") &" </a> " %> <center> <table> <tr> <td> Author: <%=rs ("name")%></td> ; <td>Email:<%=Email%></td> <td> time: <%=rs ("Newdate")%></td> </tr> <tr> <td colspan= "3" > Theme: <%=rs ("subject")%></td> </tr> <tr> <td colspan= "3" ><%=words%></td> & Lt;/tr> </table> </center> <% End Sub %> |
This program is just a simple example, there are many functions are not joined, but for everyone as a reference.