(二)添加和管理每天的新闻内容
当进行了新闻提交后,则交由一个名为addnew.asp的asp程序来对新闻内容进行处理,以便分类和保存,为了显示清析,我们每提交一条新闻,下面的那个新闻内容库就重新读入,以便可以查看新闻是否能成功加入都数据库中,也可以放便地删除新闻内容。
现在看看addnew.asp是如何完成程序处理的。
<% @language="vbscript" %>
<%
response.buffer=true
Response.Expires=0
'保存数据
session("title")=request.form("title")
session("comment")=request.form("comment")
session("pic")=request.form("pic")
session("class_name")=request.form("class_name")
session("head")=request.form("head")
'判断传过来的参数符不符合要求
if request.form("title")="" then
response.redirect "delete.asp"
end if
if request.form("comment")="" then
response.redirect "delete.asp"
end if
if request.form("class_name")="" then
response.redirect "delete.asp"
end if
select case request.form("class_name")
case "市场风云"
class=1
case "IT新闻"
class=2
case "广州市场"
class=3
case "保 留"
class=4
case "保 留"
class=4
end select
'连接数据库
%>
<!--#include Virtual="/news/data/data.inc"-->
<%
Set rs = Server.CreateObject("ADODB.Recordset")
sql="select * from data order by news_id ASC"
rs.open sql,conn,3,2
'查找编号
if rs.bof then
reco=0
else
rs.movelast
reco=Clng(rs("news_id"))+1
end if
rs.addnew
rs("news_id")=reco
if request.form("head")="YES" then
rs("news_head")=-1
end if
rs("news_class")=class
rs("news_class_name")=request.form("class_name")
if request.form("pic")<>"" then
rs("news_pic")=request.form("pic")
rs("news_title")=request.form("title")&"(图文)"
else
rs("news_title")=request.form("title")
end if
if request.form("head")="YES" then
rs("news_head")=-1
end if
tmpmess=request.form("comment")
tmpmess=replace(tmpmess,chr(10)," <br>")
tmpmess=replace(tmpmess,"<","《")
tmpmess=replace(tmpmess,">","》")
tmpmess=replace(tmpmess,"《br》","<br>")
rs("news_comment")=tmpmess
rs("news_year")=year(Date)
rs("news_month")=month(Date)
rs("news_day")=day(Date)
if Instr(Time,"下午")>0 then
tmp=left(right(Time,8),2)+12
rs("news_time")=tmp&right(Time,6)
else
rs("news_time")=tmp&right(Time,8)
end if
rs("news_delete")=0
rs.update
rs.close
response.redirect "delete.asp"
%>
程序中首先判断了提交的新闻是否合符条件,比如新闻标题是不能为空的,而换行符也得转换为html语句的br,否则不能正常地显示出新闻的格式,而这里采用了一个包含