留言本(ASP+ACCESS)实现

最近到一家网络公司实习,技术总监叫我用ASP+ACCESS做一个留言本。花了一天半时间终于做好了,东西很粗糙,只实现了基本的留言与回复功能。在这里很大家分享下希望对初学者有帮助

以下是数据库中的两张基本表的设计

index.asp

View Code
<!--#include file="inc/dbconnection.vbs"-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>留言本</title>
</head>
<body>
<table border=1 cellspacing=0 cellpadding=0 align=center width=400 >
<tr><td align=center height=100 bgcolor=#0000ff style="font-size:30px;line-height:30px">
<font color=#ffffff face="黑体" >简约不简单-我的留言本 </font>
</td></tr>

<tr><td height=20>
&nbsp;<a href=message.asp>[写留言]</a>&nbsp;&nbsp;<a href=login.asp>[管理员登陆]</a>
</td></tr>

<tr><td>
<%
set rst=Server.CreateObject("adodb.recordset")
sql
="select * from message order by time desc"
rst.Open sql,dbconnection,
1,1
number
=rst.recordcount '--获取记录总数
if number=0 then
response.write(
"没有留言!")
else
for i=1 to number
response.write(
"<tr><td height=80>")
response.write(rst(
"name")&"")
response.write(rst(
"time"))
response.write(
"留言:<br>"&rst("content"))
if rst("reply_id")<>"" then
response.write(
"<br><font color=red>&nbsp;管理员["&rst("reply_id")&"]回复:<br>"&rst("reply_content") )
end if
response.write(
"</td></tr>")
rst.movenext
'--指向下一条留言
next
end if

'--关闭数据库连接--
dbconnection.close
set dbconnection=nothing
%
>
</td></tr>

<tr><td height=60 bgcolor=#0000ff align=center>
<font color=#ffffff>版权所有:Freeze<br>E-mail:zhoujiebin15@qq.com
</td></tr>
</table>
</body>
</html>

login.asp

View Code
1 <!--#include file="inc/dbconnection.vbs"-->
2  <%
3 dim admin_id,admin_password
4 '获取管理员id与对应密码
5   admin_id=request.form("admin_id")
6 if admin_id<>"" Then
7 id=cint(admin_id) '这个地方我不知道该说什么好,为什么要强制转换类型才能进行字符串拼接
8   admin_password=request.form("admin_password")
9 sql="select * from admin where admin_id="&id&" and admin_password='"&admin_password&"'"
10 set rs=dbconnection.execute(sql)
11 if rs.eof or rs.bof then
12 response.write("<script language=javascript>alert('登录用户名或者密码错误,请重新填写!');history.go(-1) </script>")
13 else
14 '--通过session记录登陆状态 每次只能有一个管理员在线
15   session("admin")=id
16 session.timeout=60
17 response.write("<script language=javascript>location.href='manage.asp?wid="&id&"';</script>")
18 end if
19 end if
20 '--关闭数据库连接--
21   dbconnection.close
22 set dbconnection=nothing
23 %>
24  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
25  <html xmlns="http://www.w3.org/1999/xhtml">
26  <head>
27 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
28 <title>管理员登陆</title>
29 </head>
30 <body>
31 <table border=1 cellspacing=0 cellpadding=0
32 style="border-collapse:collapse" align=center width=400 bordercolor=black height=358>
33 <tr><td height=100 bgcolor=#ff0000 align=center>
34 <font style="font-size:30px"color #fffff face="黑体" >简约不简单-我的留言本</font>
35 </td></tr>
36 <tr><td height=25>
37 &nbsp;<a href=index.asp>[首页]</a>
38 </td><tr>
39 <tr><td height=178>
40 <form method="post" action="login.asp">
41 <table border="1" width="95%" id="table1" cellspacing="0" cellpadding="0"
42 bordercolor="#808080" style="border-collapse" height=154>
43 <tr><td colspan=2 height=29><p align="center" >欢迎管理员登陆</td></tr>
44 <tr><td width="32%"><p align="right">管理员id</td>
45 <td width="67%"><input type="text" name="admin_id" size="20"></td>
46 </tr>
47 <tr><td width="32%">
48 <p align="right">&nbsp;码</td>
49 <td width="67%"><input type="password" name="admin_password" size="20"></td>
50 </tr>
51 <tr><td width="99%" colspan=2>
52 <p align="center"><input type="submit" value="登陆" name="B1"></p></td>
53 </tr>
54 </table>
55 </form>
56 </td></tr>
57 <tr><td height=58 bgcolor=#ff0000 align=center>
58 <font color=#ffffff>版权所有:Freeze<br>E-mail:zhoujiebin15@qq.com
59 </td></tr>
60 </table>
61 </body>
62 </html>

logout.asp

View Code
1 <%
2 session.Abandon() '抛弃所有session变量
3 response.write("<script language=javascript>location.href='index.asp';</script>")
4 %>
5 <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
6 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
7 <html xmlns="http://www.w3.org/1999/xhtml">
8 <head>
9 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
10 <title>注销</title>
11 </head>
12
13 <body>
14 </body>
15 </html>

manage.asp

View Code
1 <!--#include file="inc/session_check.vbs"-->
2 <!--#include file="inc/dbconnection.vbs"-->
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
6 <title>留言管理</title>
7 </head>
8 <body>
9 <table border=1 cellspacing=0 cellpadding=0
10 style="border-collapse:collapse" align=center width=400 bordercolor=black height=382>
11 <tr><td height=100 bgcolor=#00ff00 align=center
12 style="font-size:30px;line-height:30px">
13 <font color=#ffffff face="黑体">简约不简单-我的留言本 </font></td>
14 </tr>
15 <tr><td height=25>
16 <a href=logout.asp>[注销登陆]</a>
17 </td></tr>
18 <tr><td >
19 <%
20 set rst=Server.CreateObject("adodb.recordset")
21 sql="select * from message order by time desc"
22 rst.Open sql,dbconnection,1,1
23 number=rst.recordcount
24 if number=0 then
25 response.write("没有留言!")
26 else
27 for i=1 to number
28 response.write("<tr><td height=60>")
29 response.write(rst("name")&"")
30 response.write(rst("time"))
31 response.write("留言:&nbsp;&nbsp;&nbsp;&nbsp;")
32 if rst("reply_id")<>"" then
33 else
34 response.write("<a href=reply.asp?wid="&wid&"&note_id="&rst("note_id")&">[回复]</a>")
35 end if
36 response.write("<br>"&rst("content"))
37 response.write("</td></tr>")
38
39 rst.movenext
40 next
41 end if
42
43 '--关闭数据库连接--
44 dbconnection.close
45 set dbconnection=nothing
46 %>
47 </td></tr>
48 <tr><td height=60 bgcolor=#00ff00 align=center>
49 <font color=#ffffff>版权所有:Freeze <br>E-mail:zhoujiebin15@qq.com
50 </td></tr>
51 </table>
52 </body>
53 </html>

message.asp

View Code
1 <!--#include file="inc/dbconnection.vbs"-->
2 <%
3 dim name,content,time
4 '获取留言信息
5 name=trim(request.form("name"))
6 content=trim(request.form("content"))
7 if name<>"" and content<>"" then
8 time=now() '--获取系统时间
9 sql="insert into message([name],[content],[time]) values('"&name&"','"&content&"','"&time&"')"
10 dbconnection.execute(sql)
11 '--关闭数据库连接
12 dbconnection.close
13 set dbconnection=nothing
14 response.write "<script>alert('留言提交成功!');window.location.href='index.asp';</script>"
15 end if
16 %>
17
18 <html xmlns="http://www.w3.org/1999/xhtml">
19 <head>
20 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
21 <title>写留言</title>
22 </head>
23 <body>
24 <table border=1 cellspacing=0 cellpadding=0
25 style="border-collapse" align=center width=400 bordercolor=black >
26 <tr><td align=center height=100 bgcolor=#0000ff>
27 <font style="font-size:30px" color=#ffffff face="黑体">简约不简单-我的留言本 </font>
28 </td></tr>
29 <tr><td height=25 >&nbsp;<a href=index.asp>[首页]</a>
30 </td></tr>
31 <tr><td height=200>
32 <form method="post" action="message.asp">
33
34 <table border="1" width="95%" id="table1" cellspacing="0" cellpadding="0" bordercolor="#808080"
35 style="border-collapse:collapse" height="265">
36 <tr><td colspan="2" height="29"><p align="center">欢迎您填写留言 </td></tr>
37 <tr><td width="32%" ><p align="right">昵称 </td>
38 <td width="67%"><input type="text" name="name" size="20"></td>
39 </tr>
40 <tr><td width="32%"><p align="right">留言内容</p></td>
41 <td width="67%"><textarea rows="10" name="content" cols="31"></textarea></td>
42 </tr>
43 <tr><td width="99%" colspan="2"><p align="center" >
44 <input type="submit" value="提交" name="B1"></p></td></tr>
45 </table>
46 </form>
47 </td></tr>
48 <tr><td height=80 bgcolor=#0000ff align=center >
49 <font color=#FFFFFF >版权所有:Freeze<br>E-mail:zhoujiebin15@qq.com
50 </td></tr>
51 </table>
52
53 </body>
54 </html>

reply.asp

View Code
1 <!--#include file="inc/session_check.vbs"-->
2 <!--#include file="inc/dbconnection.vbs"-->
3 <%
4 '--获取FORM与GET数据
5 reply_content=request.form("reply")
6 note_id=int(request.QueryString("note_id"))
7 if reply_content<>"" then
8 sql="update message set reply_id="&wid&",reply_content='"&reply_content&"' where note_id="&note_id
9 'response.write(sql)
10 dbconnection.execute(sql)
11 response.write "<script>alert('回复留言成功!');window.location.href='manage.asp?wid="&wid&"';</script>"
12 end if
13 '--关闭数据库连接
14 dbconnection.close
15 set dbconnection=nothing
16 %>
17 <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
18 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
19 <html xmlns="http://www.w3.org/1999/xhtml">
20 <head>
21 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
22 <title>回复</title>
23 </head>
24
25 <body>
26 <table border=1 cellspacing=0 cellpadding=0
27 style="border-collapse:collapse" align=center width=400 bordercolor=black >
28 <tr><td height=100 bgcolor=#ffff00 align=center>
29 <font style="font-size:30px" color=#ffffff face="黑体">简约不简单-我的留言本</font>
30 </td></tr>
31 <tr><td height=25>
32 <% response.Write("<a href=manage.asp?wid="&wid&">[管理留言]</a>") %>
33 </td></tr>
34 <tr><td height=150>
35 <% response.Write("<form method=post action=reply.asp?wid="&wid&"&note_id="&note_id&">") %>
36 <table border=1 width="95%" id="table1" cellspacing="0" cellpadding=0 bordercolor="#808080"
37 style="border-collapse:collapse" height=154>
38 <tr><td colspan=2 height=29>
39 <p align="center">管理员回复留言</td>
40 </tr>
41 <tr><td width="32%">
42 <p align="left">请输入回复</td>
43 <td width="67%"><textarea rows=7 name="reply" cols="33" ></textarea></td>
44 </tr>
45 <tr><td width="99%" colspan="2">
46 <p align="center"><input type="submit" value="确认" name="B1"></p>
47 </td></tr>
48 </table>
49 </form>
50 </td></tr>
51 <tr><td height=58 bgcolor=#ffff00 align=center>
52 <font color=#ffffff>版权所有:Freeze<br>E-mail:zhoujiebin15@qq.com
53 </td></tr>
54 </table>
55 </body>
56 </html>

dbconnection.vbs

View Code
1 <%
2 '--建立数据库连接--
3 set dbconnection=Server.CreateObject("adodb.connection")
4 'dbconnection.Open "driver={Microsoft Access Driver (*.mdb)};dbq="&Server.MapPath("note.mdb")
5 dbconnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("note.mdb")
6 If Err Then
7 err.Clear
8 Set conn = Nothing
9 Response.Write "数据库连接出错!"
10 Response.End
11 End If
12 %>

session_check.vbs

View Code
1 <%
2 '--登陆检验脚本--
3 wid=request.querystring("wid")
4 if cint(session("admin"))<>cint(wid) then
5 response.write("<script language=javascript>alert('请重新登陆,再管理!');location.href='login.asp';</script>")
6 end if
7 %>

最后不得不说下自己在制作过程中碰到的问题,由于自己以前学过C、java、php、mysql这次是头一次用asp和access,在一些问题上浪费了本不该浪费的时间

1 asp中注释符号用的是单引号' 脚本用<%与%>包括起来

2 asp中的循环控制语句if,while,case等语法变动较大

3 字符串拼接用&符号

4 access中插入语句"insert into message([name],[content],[time]) values....不要忘了中括号

5 asp连接数据库感觉真烦,远没有php来的方便

6 记得把浏览器中的错误报告开起来

7 自己机子有点残废不能装IIS服务器,上网找了个小旋风AspWeb服务器2005,还不错

8 asp中form与get数据用request对象进行获取

9 asp中执行sql语句有两种方式一个是execute还有一个是open方式,我被搞得稀里糊涂的

原文地址:https://www.cnblogs.com/2010Freeze/p/2056783.html