用ASP+Access制作论坛教程

2016 年 9 月 3 日4030

access|asp+|教程

  在网上介绍如何编写制作论坛的文章不少,但据我观察大多数的代码都有不同程度的错误,会误导大家。所以我写这篇文章的目的就是把经过我测试成功的代码与思路提供给大家。下面就让我们从数据库的建立开始:

  用access建立数据库:

  首先我们要建立一个存放帖子的数据库,在这里我介绍用access2000建立数据库的方法。

  我们先要建立一个名为news.mdb的数据库文件,然后在其中点“新建”选择“设计视图”建立两个表,一个名为details用来存放回复,另一个名为titles用来存放主题如图:

  制作过程详解:

  在titles中建立以下字段,如图:

  注意:

1 右键单击“titleID”选择“主键”,出现钥匙图形。 2 这里的“shu”不可以改为“number”否则会出现错误,王国容的那本《asp & web 数据库》中就犯了这样的错误。 3 将“日期/时间”类型的字段的默认值都设为now()

  在titles中建立以下字段,如图:

  将“日期/时间”类型的字段的默认值都设为now()。建立完成后,要设置这两个表的关联。点击

按钮,出现如下对话框:


  将titles中的titleID拖至details中的titleID上松手,出现如下对话框:(如图设置)

  然后保存就可以了。这样一个提供储存的数据库就建立好了。接下来我们要编写asp程序了。首先让我们先来分析论坛所需要的功能。大概需要以下几个功能:

1显示主题 2发布主题 3显示回复 4发布回复

  于是,我们根据这些功能,需要编写6个asp文件:
  title.asp、titlenew.asp、titleout.asp、detail.asp、detnew.asp、detout.asp
  还有一个文件是必需的(在我的程序中)adovbs.inc,它提供程序中的常数值如:“adopenstatic”。(点击这里下ADOVBS.INC载源文件)每个文件的代码:

  title.asp:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="CP_ACP"%>
<!--#include virtual="adovbs.inc"-->
<!--#include file="titleout.asp"-->
<%
    const head="我的论坛"
    dbpath=server.MapPath("news.mdb")
    set conn=server.CreateObject("adodb.connection")
    conn.open"driver={Microsoft Access Driver (*.mdb)};dbq="&dbpath
    sql="select titleID,createdate as 主题发布时间,lastnewsdate as 最后回复时间,name as 作者,shu as 回复篇数,subject as 主题 from titles order by lastnewsdate desc"
    set rs=server.CreateObject("adodb.recordset")
    rs.open sql,conn,adopenstatic
%>
<html>
<head>
<title><%=head%></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
body { font-family: "宋体"; font-size: 12px}
table { font-family: "宋体"; font-size: 12px}
a:link { font-family: "宋体"; font-size: 12px; color: #000000; text-decoration: none}
a:visited { font-family: "宋体"; font-size: 12px; color: #666666; text-decoration: none}
a:hover { font-family: "宋体"; font-size: 12px; color: #ff3300; text-decoration: underline}
-->
</style>
</head>
<body>
<!--输出主题-->
<% 
           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 cellpadding="0" cellspacing="0">
       <tr>
           <td>
    <%
       if page<>1 then
           response.Write("<a href=title.asp?page=1>第一页</a>")
           response.Write("<a href=title.asp?page="&(page-1)&">上一页</a>")
       end if
       if page<>rs.pagecount then
           response.Write("<a href=title.asp?page="&(page+1)&">下一页</a>")
           response.Write("<a href=title.asp?page="&rs.pagecount&">最后一页</a>")
       end if
    %>
    转到第<input type="text" size="3">页
    页码:<font color="#FF0000"><%=page%></font>/<font color="#FF0000"><%=rs.pagecount%></font>
           </td>
       </tr>
    </table>
</form>
<center>
<table bgcolor="#999999" cellpadding="3" cellspacing="1">
    <tr bgcolor="#00FFFF">
       <td>主题发布时间</td>
       <td>主题</td>
       <td>作者</td>
       <td>回复</td>
       <td>最后回复时间</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 cellpadding="0" cellspacing="0">
       <tr>
           <td>
    <%
       if page<>1 then
           response.Write("<a href=title.asp?page=1>第一页</a>")
           response.Write("<a href=title.asp?page="&(page-1)&">上一页</a>")
       end if
       if page<>rs.pagecount then
           response.Write("<a href=title.asp?page="&(page+1)&">下一页</a>")
           response.Write("<a href=title.asp?page="&rs.pagecount&">最后一页</a>")
       end if
    %>
    转到第<input type="text" size="3">页
    页码:<font color="#FF0000"><%=page%></font>/<font color="#FF0000"><%=rs.pagecount%></font>
           </td>
       </tr>
    </table>
</form>
<!--下面为输入表单-->
<form action="titlenew.asp" method="post">
    <center>
       <table>
           <tr>
              <td>姓名:</td>
              <td><input type="hidden" size="30" value="<%=session("name")%>"><%=session("name")%></td>
           </tr>
           <tr>
              <td>信箱:</td>
              <td><input type="text" size="30" value="<%=session("Email")%>"></td>
           </tr>
           <tr>
              <td>主题:</td>
              <td><input type="text" size="60"></td>
           </tr>
           <tr>
              <td>内容:</td>
              <td><textarea rows="8" cols="60"></textarea></td>
           </tr>
           <tr>
              <td colspan="2"><input type="submit" value="提交">
              <input type="reset" value="清空"></td>
           </tr>
       </table>
    </center>
</form>
</body>
</html>

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)};dbq="&dbpath
    name=request("name")
    Email=request("Email")
    subject=request("subject")
    words=request("words")
    if subject="" or words="" or or Email="" then
       outputmsg="字段不能为空,请填写完整信息!"
    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="您的主题已加入!"
       session("name")=name
       session("Email")=Email
    end if
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="refresh" content="3;URL=title.asp">
</head>
<body>
<%=outputmsg%>
</body>
</html>
titleout.asp:

<%
    sub titleoutput(rs)
%>
    <tr bgcolor="#ffffff">
       <td valign="top"><%=rs("主题发布时间")%></td>
       <td valign="top"><a href="detail.asp?titleID=<%=rs("titleID")%>"><%=rs("主题")%></a></td>
       <td valign="top"><%=rs("作者")%></td>
       <td valign="top"><%=rs("回复篇数")%></td>
       <td valign="top"><%=rs("最后回复时间")%></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)};dbq="&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
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
    words=replace(" "&rs("words"),chr(13),"<br>")
    Email="<a href="""&rs("Email")&"mailto:"&rs("Email")&""">"&rs("Email")&"</a>"
%>
<center>
    <table>
       <tr>
           <td>作者:<%=rs("name")%></td>
           <td>Email:<%=Email%></td>
           <td>时间:<%=rs("createdate")%></td>
       </tr>
       <tr>
           <td colspan="3">主题:<%=rs("subject")%></td>
       </tr>
       <tr>
           <td colspan="3"><%=words%></td>
       </tr>
    </table>
</center>
<%
    while not rsdetail.eof
       detailoutput rsdetail
       rsdetail.movenext
    wend
%>
<center>发表回复</center>
<form action="detnew.asp" method="post">
    <input type="hidden" value="<%=Request("TitleID")%>">
    <center>
       <table>
           <tr>
              <td>姓名:</td>
              <td><input type="text" size="30" value="<%=session("name")%>"></td>
           </tr>
           <tr>
              <td>信箱:</td>
              <td><input type="text" size="30" value="<%=session("Email")%>"></td>
           </tr>
           <tr>
              <td>主题:</td>
              <td><input type="text" size="60"></td>
           </tr>
           <tr>
              <td>内容:</td>
              <td><textarea rows="8" cols="60"></textarea></td>
           </tr>
           <tr>
              <td colspan="2"><input type="submit" value="提交">
              <input type="reset" value="清空"></td>
           </tr>
       </table>
    </center>
</form>
<center><a href="title.asp">返回主题页</a></center>
</body>
</html>
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)};dbq="&dbpath
    titleID=request("titleID")
    name=request("name")
    Email=request("Email")
    subject=request("subject")
    words=request("words")
    if subject="" or words="" or or Email="" then
       outputmsg="字段不能为空,请填写完整信息!"
    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="您的回复已加入!"
       session("name")=name
       session("Email")=Email
      sql="update titles set lastnewsdate=now(),shu=shu+1 where titleID="&clng(titleID)    
       conn.execute(sql)
    end if
%>
<html>
<head>
<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%>">
</head>
<body>
<center><%=outputmsg%></center>
</body>
</html>
detout.asp

<%
    sub detailoutput(rs)
       words=replace(server.HTMLEncode(" "&rs("words")),chr(13),"<br>")
       words=replace(words," ","")
       Email="<a href="""&rs("Email")&"mailto:"&rs("Email")&""">"&rs("Email")&"</a>"
%>
<center>
    <table>
       <tr>
           <td>作者:<%=rs("name")%></td>
           <td>Email:<%=Email%></td>
           <td>时间:<%=rs("newdate")%></td>
       </tr>
       <tr>
           <td colspan="3">主题:<%=rs("subject")%></td>
       </tr>
       <tr>
           <td colspan="3"><%=words%></td>
       </tr>
    </table>
</center>
<%
    end sub
%>
这个程序只是一个简单的范例,还有许多功能没有加入,只是给大家作为参考。

更多关于 的文章

·上一篇:ASP中数据库安全问题之我见

·下一篇:正则表达式在网页处理中的应用四则

0 0