<%@LANGUAGE="JAVASCRIPT" CODEPAGE="936"%>
<%
//创建数据库连接
var connstr = 'Provider = Microsoft.Jet.OLEDB.4.0;Data Source = ' + Server.mapPath("book.mdb"); 
conn=Server.createobject("adodb.connection");
conn.open(connstr);

//判断管理员权限，写入Session
if(Request("Submit1")=="提交"){
	rs=Server.CreateObject("adodb.recordset"); 
	sql="select * from admin where user='" + Request.Form("user") + "' and psw='" + Request.Form("psw") + "'";
	rs.open(sql,conn,1);	
	if(rs.BOF && rs.EOF){   //如果按用户名和密码双重条件不能取出记录，说明表中无此记录
		Session("in") = "no";
		//Response.Write("密码错误");
	}else{
		Session("in") = "ok"; 
		//Response.Write("通过");
	}
	Response.Redirect("index.asp");
}

//删除记录
if(Request.QueryString("id").Item){
	sc="delete from guestbook where id=" + Request.QueryString("id"); 
	conn.Execute(sc); 
	sc=null;
	Response.Redirect("index.asp");
}

//判断是否提交留言，并增加记录
if(Request("Submit2")=="提交"){  //判断按钮Submit2是否被点击，如果是，则执行后面的代码

	rs=Server.CreateObject("adodb.recordset");
	rssql="select * from guestbook";
	rs.Open (rssql,conn,1,3 );
	rs.AddNew;
		
	rs("name")= Request.Form("name");
	rs("content")= Request.Form("content"); 

	rs.Update;
	rs.close();
	rs=null;
	
	Response.Redirect("index.asp");
}

//创建留言列表用的记录集
rs=Server.createobject("adodb.recordset"); 
rssql="select * from guestbook order by id desc";
rs.open(rssql,conn,1);

//翻页
if(!rs.EOF){  //判断记录集是否为空，如果不为空，则定义翻页参数
	rs.PageSize = 2; //设定每页显示的记录条数
	var page=1; //设定页码的初始值为第1页
		if(Request.QueryString("p").Item){	 //判断路径中是否有页码参数"p"
			page = Request.QueryString("p"); 
			if(page<1){ 
				page=1;
			}else if(page>rs.PageCount){
				page=rs.PageCount;
			}
		}
	rs.AbsolutePage = page;
}
%>	
	
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>ASP留言板练习</title>
<style type="text/css">
<!--
.STYLE1 {
	color: #000099;
	font-size: 36px;
	font-weight: bold;
}
body,td,th {
	font-size: 12px;
}
.STYLE2 {
	color: #000000;
	font-weight: bold;
}
-->
</style>
</head>

<body>
<p align="center" class="STYLE1">ASP留言板练习</p>
<hr size="1" noshade="noshade" />

<%
if(Request.QueryString("fun")=="login"){  //判断是否显示登录框
%>

<form id="form1" name="form1" method="post" action="">
  <table width="600" border="1" align="center" bordercolor="#CCCCCC" style="border-collapse:collapse">
    <tr>
      <td height="35" colspan="2" align="center" bgcolor="#999999">管理员登录</td>
    </tr>
    <tr>
      <td align="center">用户名
        <input name="user" type="text" id="user" /></td>
      <td width="81" rowspan="2" align="center"><input name="Submit1" type="submit" id="Submit1" value="提交" /></td>
    </tr>
    <tr align="center">
      <td>密&nbsp;&nbsp;码
      <input name="psw" type="text" id="psw" /></td>
    </tr>
  </table>
</form>
<%
}else{
%>
<form id="form2" name="form2" method="post" action="">
  <table width="600" border="1" align="center" bordercolor="#CCCCCC" style="border-collapse:collapse">
    <tr>
      <td height="35" colspan="2" align="center" bgcolor="#999999">撰写留言</td>
    </tr>
    <tr>
      <td width="386" rowspan="2" align="center"><textarea name="content" cols="50" rows="5" id="content"></textarea></td>
      <td width="198" align="center">网名：
      <input name="name" type="text" id="name" /></td>
    </tr>
    <tr>
      <td align="center"><input type="submit" name="Submit2" value="提交" /></td>
    </tr>
  </table>
</form>

<% for(i=1;i<=rs.PageSize && !rs.EOF;i++){   //循环列表和判断页码 %>
<table width="600" border="1" align="center" cellpadding="5" bordercolor="#999999" style="border-collapse:collapse">
  <tr>
    <td align="left" bgcolor="#999999"><span class="STYLE2"><%=rs("name")%></span> 的留言 | <%=rs("time")%></td>
    <td width="50%" align="right" bgcolor="#999999">
	<%	if(Session("in")=="ok"){ %>
	<a href="index.asp?id=<%=rs("id")%>">删除</a>
	<% 	}	%>
	</td>
  </tr>
  <tr>
    <td colspan="2" align="left" valign="top"><%=rs("content")%></td>
  </tr>
</table>
<%	rs.MoveNext; } %>
<br />
<table width="600" align="center">
  <tr>
    <td align="center">	
	<a href="index.asp?p=<%=1%>">首页</a> ｜ 
	<a href="index.asp?p=<%=page-1%>">上一页</a> ｜	  
	<a href="index.asp?p=<%=Number(page)+1%>">下一页</a> ｜ 
	<a href="index.asp?p=<%=rs.PageCount%>">尾页</a> 
	</td>
  </tr>
  <tr>
    <td align="center">
	<% for(k=1;k<=rs.PageCount;k++){ %>
	<a href="index.asp?p=<%=k%>"><%=k%></a>	
	<% } %> 
	</td>
  </tr>
</table>
<hr size="1" noshade="noshade" />
<table width="600" align="center">
  <tr>
    <td align="right"><a href="index.asp?fun=login">管理员登录</a></td>
  </tr>
</table>
<% } %>
</body>
</html>

<%
//关闭数据库连接及清空记录集
rs.Close();
rs=null;
conn.Close();
conn=null;
%>
