5分钟编写一个博客(使用ASP技术)
2018-09-06 11:55
  一、建立文件!
 最简单的论坛也要有几个必要的文件,就是:
 1、数据库,用来储存发表的帖子!-------命名为:bbs.mdb
 2、有数据库就必须连接数据库,该文件用来连接数据库:----命名为:conn.asp
 3、发表帖子的文件----命名为:say.asp
 4、保存发表帖子内容的文件-----命名为:save.asp
 5、显示帖子标题的文件------命名为:index.asp
 6、显示帖子内容的文件,即点击标题后所进入的页面-----命名为:show.asp
 二、各文件的主要内容
 1、bbs.mdb
 打开这个数据库,建立一个表,命名为bbs
 该表中有几个字段:
 bid(自动给帖子编号),他的数据类型设为自动编号
 bname(用来储存发表的作者),数据类型为文本
 btitle(用来储存帖子的主题),数据类型为文本
 bbody(用来储存帖子的内容),数据类型为备注
 2、conn.asp
 源代码为:
 <%
 server.ScriptTimeout="10"
 '设置或返回在某脚本终止前它所能运行时间(秒)的最大值
 conn.open="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source="server.MapPath("bbs.mdb")
 %>
 3、say.asp
 源代码为:
 <form method="post" action="save.asp">
 <p>
 大名:<input type="text" name="bname" size="20">
 </p>
 <p>
 标题:<input type="text" name="btitle" size="91">
 </p>
 <p> 内容:</p>
 <p>
 <textarea rows="11" name="bbody" cols="97"></textarea>
 </p>
 <p></p>
 <p>
 <input type="submit" value="提交" name="b1">
 <input type="reset" value="重置" name="b2">
 </p>
 </form>
 大家应该看得懂的,这个根本就是html语法,对的,根本就是,这个只是发表的界面
 不过要注意这一句<form method="POST" action="save.asp">,他把用户所填的内容发送至save.asp这个文件,下面就看save.asp这个文件吧!
 4、save.asp
 源代码:
 <!--#include file="../conn.asp"-->
 <%
 bname=Request.Form("bname")
 btitle=Request.Form("btitle")
 bbody=Request.Form("bbody")
 %>
 <%
 if bname="" or btitle="" or bbody="" then
 %>
 请<a href="javascript:history.back(-2)">后退</a>填写完整资料,你才能发表帖子!
 <%else%>
 <%
 set savebbs=conn.execute("insert into bbs(bname,btitle,bbody) values ('"bname"','"btitle"','"bbody"')")
 %>
 发表成功!<a href="index.asp">查看贴子</a>
 <%
 end if
 set savebbs=nothing
 %>第一句:<!--#include file="conn.asp"-->,意思是插入conn.asp文件,即与数据库进行连接!
 asp语句必须用<% %>这个格式包含起来,而插入文件的语句就不用,
 接下来几句:
 name=Replace(Request.Form("name"),"'","''")
 title=Replace(Request.Form("title"),"'","''")
 body=Replace(Request.Form("body"),"'","''")
 Replace(Request.Form("name"),"'","''")意思是接收表单中名为name的文本框发来的数据,
 而name=Replace(Request.Form("name"),"'","''")则是把发来的数据储存在name变量中,
 如果你这样:abc123=Replace(Request.Form("name"),"'","''")
 就是把表单中名为name的文本框发来的数据储存在abc123变量中
  接着:if name="" or title="" or body="" then
 判断name、title和body变量中是否没有填写内容,即为"",如果这样,就执行这一语句:
 请<a href="****:history.go(-1)">后退</a>填写完整资料,你才能发表帖子!
 该语句属于html语法,大家都看得懂的!
 "<%else%>"就是说当if……then不成立(即是说所有内容都已经填写),就执行语句:
 <%set savebbs=conn.execute("insert into bbs(name,title,body) values('"name"','"title"','"body"')")%>
 发表成功!<a href="index.asp">查看帖子</a>
 "set save=conn.execute"属于固定的语句,不过savebbs可以自己修改,"="后面的就难解释了:
 insert into bbs(name,title,body)意思是向名为bbs的表(在建立数据库时已经建立的表)
 中的name,title,body字段插入,插入什么呢?看接下的values('"name"','"title"','"body"')")
 values是值的意思
 就是插入向量name,title,body,向量用格式'"name"'表示
 最后:end if就是结束if……then
 set savebbs=nothing可以说是关闭掉:
 set savebbs=conn.execute("insert into bbs(name,title,body)
 values('"name"','"title"','"body"')")
 5、index.asp
 源代码:
 <!--#include file="../conn.asp"-->
 <b><a href="../say.asp">发表帖子</a></b>
 <br><br><br>
 <p align="center">
 <center>
 <table border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse;" bordercolor="#000000" width="100%" height="26">
 <tr>
 <td width="17%">
 <b>作者</b>
 </td>
 <td width="83%">
 <b>主题</b>
 </td>
 </tr>
 </table>
 </center>
 </p>
 <hr size="1">
 <%
 i=1
 set showbbs=conn.execute("select * from bbs order by bid desc")
 do while not showbbs.eof
 %>
 <p align="center">
 <center>
 <table border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse;" bordercolor="#000000" width="100%" height="26">
 <tr>
 <td width="17%">
 <%=showbbs("bname")%>
 </td>
 <td width="83%">
 <a href="../show.asp?bid=<%=showbbs("bid")%>">
 <%=showbbs("btitle")%>
 </a>
 </td>
 <tr>
 </table>
 </center>
 </p>
 <hr size="1">
 <%
 i=i+1
 if i>50 then exit do
 showbbs.movenext
 loop
 showbbs.close
 set showbbs=nothing
 %>
 这个文件就不一句一句的讲了
 主要讲精华部分:
 set showbbs=conn.execute("select*from bbs order by id desc")
 意思是:向数据库中的bbs数据表查询数据,并以id排顺序,
 还有这么一句:<%=showbbs("name")%>
 就是显示数据表中的name字段的数据,这里的showbbs就是set showbbs=……中的showbbs
 代码中的i=1和i=i+1
 if i>50 then exit do
 showbbs.movenext
 Loop
 showbbs.Close
 set showbbs=nothing
 这几句属于循环语句,这里就不理他,理解了也不太好用,因为他只显示50张贴子!
 if i>50 then exit do中的50可以修改
 但我们做论坛必须把帖子分页,又因为分页这个语句太复杂,我想就不讲了,等这一个弄懂了才来弄
 还有一句很有用的:
 <a href="show.asp?id=<%=showbbs("id")%>"><%=showbbs("title")%></a>
 里面的超连接:show.asp?id=<%=showbbs("id")%>,注意:这里的超连接把帖子的id包含了,
 等一下在show.asp文件中就有用了
 5、show.asp
 源代码:
 <!--#include file="../conn.asp"-->
 <%
 bid=Request.QueryString("bid")
 %>
 <%
 set showbbs=conn.execute("select * from bbs where bid="bid"")
 %>
 <a href="index.asp">
 <b>回到首页</b>
 </a>
 <br><br>
 <b>
 <a href="say.asp">
 发表帖子lt;
 </a>
 </b><br>
 <hr size="1">
 <table border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse;" bordercolor="#111111" width="100%" height="26">
 <tr>
 <td width="21%" headers="22">
 <b>作者:</b><%=showbbs("bname")%>
 </td>
 <td width="79%" headers="22">
 <b>主题:</b><%=showbbs("btitle")%>
 </td>
 </tr>
 <tr>
 <td width="100%" colspan="2" headers="158" valign="top">
 <b>内容:</b><%=showbbs("bbody")%>
 </td>
 </tr>
 </table>
 <%
 set showbbs=nothing
 %>
 语句---精华语句:
 id=request.querystring("id")
 在讲解index.asp文件的后面已经说到:show.asp?id=<%=showbbs("id")这一句,
 id=request.querystring("id")就是把地址栏中的id的值读取下来,
 因为index.asp文件中的超连接点击后,地址栏就为数字,
 所以show.asp使用id=request.querystring("id")语句把数字读取下来
 于是接着使用:set show=conn.execute("select*from bbs where id="id"")
 向数据表查询id为这时读取下来的数字的帖子,即where id="id"
 最后<%set show=nothing%>
 好了,一个简单的论坛就这样完成了,挂在我们自己的ASP空间上去试试
  
 摘自 wangganggang90的专栏