php
发表在PHP图书答疑
2009-05-14
是否精华
是
否
版块置顶:
是
否
在修改和删除的时候获取不到id
<?php
class page
{
private $pagesize;
private $page;
private $pagecount;
private $total;
private $conn;
public function __construct($pagesize,$page)
{
$this->pagesize=$pagesize;
$this->page=$page;
}
public function listInfo()
{
if($this->page=="" || !is_numeric($this->page))
{
$this->page=1;
}
$this->conn=mysql_connect("localhost","root","root");
mysql_select_db("database21",$this->conn);
mysql_query("set names gb2312");
$sql=mysql_query("select count(*) as total from tb_student",$this->conn);
$info=mysql_fetch_array($sql);
$this->total=$info[total];
if($this->total==0)
{
echo "<table width=704 height=20 border=0 align=center cellpadding=0 cellspacing=0>"+
"<tr>"+
"<td><div align=center>暂无学生信息!</div></td>"+
"</tr>"+
"</table>";
}
else
{
if(($this->total % $this->pagesize)==0)
{
$this->pagecount=intval($this->total/$this->pagesize);
}
else
{
if($this->total<=$this->pagesize)
{
$this->pagecount=1;
}
else
{
$this->pagecount=ceil($this->total/$this->pagesize);
}
}
$sql=mysql_query("select * from tb_student order by sno desc limit ".$this->pagesize*($this->page-1).",$this->pagesize",$this->conn);
?>
<table width="703" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#4C6404">
<tr>
<td width="100" height="20" bgcolor="#C2C41F"><div align="center">学号</div></td>
<td width="100" bgcolor="#C2C41F"><div align="center">姓名</div></td>
<td width="100" bgcolor="#C2C41F"><div align="center">性别</div></td>
<td width="100" bgcolor="#C2C41F"><div align="center">班级</div></td>
<td width="297" bgcolor="#C2C41F"><div align="center">住址</div></td>
<td width="300" bgcolor="#C2C41F"><div align="center">操作</div></td>
</tr>
<?php
while($info=mysql_fetch_array($sql))
{
?>
<tr>
<td height="20" bgcolor="#FFFFFF"><div align="center"><?php echo $info["sno"];?></div></td>
<td height="20" bgcolor="#FFFFFF"><div align="center"><?php echo $info["sname"];?></div></td>
<td height="20" bgcolor="#FFFFFF"><div align="center"><?php echo $info[ssex];?></div></td>
<td height="20" bgcolor="#FFFFFF"><div align="
center"><?php echo $info[sclass];?></div></td>
<td height="20" bgcolor="#FFFFFF"><div align="center"><?php echo $info[saddress];?></div></td>
<td height="20" bgcolor="#FFFFFF"><div align="center"><a href=ceshi.php?action=modify&sno=".$info[0].">修改</a>/<a href=edit.php?action=del&sno=".$info[0]." onClick="return del();">删除</a> </div></td>
</tr>
<?php
}
echo "</table>";
}
}
public function toPage()
{
?>
<table width="704" height="20" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="342">共有学生信息<?php echo $this->total;?> 条 每页显示<?php echo $this->pagesize;?>条 第 <?php echo $this->page;?>页/共<?php echo $this->pagecount;?>页</td>
<td width="362"><div align="right">
<a href="<?php echo $_SERVER["PHP_SELF"]?>?page=1">首页</a>
<a href="<?php echo $_SERVER["PHP_SELF"]?>?page=<?php
if($this->page>1)
echo $this->page-1;
else
echo 1;
?>">上一页</a>
<a href="<?php echo $_SERVER["PHP_SELF"]?>?page=<?php
if($this->page<$this->pagecount-1)
echo $this->page+1;
else
echo $this->pagecount;
?>">下一页</a>
<a href="<?php echo $_SERVER["PHP_SELF"]?>?page=<?php echo $this->pagecount;?>">尾页</a>
</div></td>
</tr>
</table>
<?php
}
public function __destruct()
{
mysql_close($this->conn);
}
}
$obj=new page(4,$_GET["page"]);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>学生信息分页显示</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script language="javascript">
function del()
{
if(!window.confirm("是否要删除数据?"))
return false;
}
</script>
</head>
<body topmargin="0" bottommargin="0" leftmargin="0">
<table width="200" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><img src="images/banner.gif" width="703" height="234"></td>
</tr>
</table><br>
<?php
$obj->listInfo();
$obj->toPage();
?>
<table width="704" height="20" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="center">417</div></td>
</tr>
</table>
</body>
</html>
<!-- edit.php页面-->
<!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>无标题文档</title>
</head>
<body>
<table width="799" height="100" align="center" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="middle">
<?php
if($_POST["action"]=="modify")
{
if(!($_POST["sname"] and $_POST["ssex"] and $_POST["sclass"] and $_POST["saddress"]))
{
echo "输入不允许为空,点击这里<a href='javascript:onclick=history.go(-1);'</a>";
}
else
{
$conn=mysql_connect("localhost","root","root");
mysql_select_db("database21");
mysql_query("set names gb2312");
$sql="update tb_student set sname='".$_POST["sname"]."',ssex='".$_POST["ssex"]."',sclass='".$_POST["sclass"]."',saddress='".$_POST["saddress"]."' where sno=".$_GET["sno"];
$result=mysql_query($sql,$conn);
}
if($result)
{
echo "修改成功,点击这里<a href='javascript:history.go(-1);'>查看</a>";
}
else
echo "失败";
}
if($_POST["action"]=="modify")
{
$conn=mysql_connect("localhost","root","root");
mysql_select_db("database21");
mysql_query("sey names gb2312");
$sql=mysql_query("select * from tb_student where sno='".$_GET["sno"]."'",$conn );
$rows=mysql_fetch_row($sql);
?>
<form name="intForm" method="post" action="edit.php">
<table width="100%" height="200" align="center" border="0" cellpadding="0" cellspacing="0">
<tr align="center" valign="middle">
<td width="30%" class="c_td"> </td>
<td width="10%" align="right" class="c_td"> </td>
<td width="30%" class="c_td"> </td>
<td width="30%" class="c_td"> </td>
</tr>
<tr>
<td class="c_td"> </td>
<td align="right" valign="middle" class="c_td">姓名:</td>
<td align="center" valign="middle" class="c_td"><input type="text" name="bookname" value="<?php echo $rows[1]; ?>"></td>
<td class="c_td"> </td>
</tr>
<tr>
<td class="c_td"> </td>
<td align="right" valign="middle" class="c_td">性别:</td>
<td align="center" valign="middle" class="c_td"><input type="text" name="price" value="<?php echo $rows[2]; ?>"></td>
<td class="c_td"> </td>
</tr>
<tr>
<td class="c_td"> </td>
<td align="right" valign="middle" class="c_td">班级:</td>
<td align="center" valign="middle" class="c_td"><input type="text" name="f_time" value="<?php echo $rows[3] ;?>"></td>
<td class="c_td"> </td>
</tr>
<tr>
<td class="c_td"> </td>
<td align="right" valign="middle" class="c_td">地址:</td>
<td align="center" valign="middle" class="c_td"><input type="text" name="type" value="<?php echo $rows[4] ;?>"></td>
<td class="c_td"> </td>
</tr>
<tr align="center" valign="middle">
<td class="c_td"> </td>
<td colspan="2" class="c_td">
<input type="hidden" name="action" value="modify">
<input type="hidden" name="sno" value="<?php echo $rows[0]; ?>">
<input type="submit" name="Submit" value="修改">
<input type="reset" name="reset" value="重置"></td>
<td class="c_td"> </td>
</tr>
</table>
</form>
<?php
}
//单条删除操作
if ($_GET["action"] == "del"){
$conn=mysql_connect("localhost","root","root");
mysql_select_db("database21");
mysql_query("sey names gb2312");
$sqlstr1 = mysql_query("delete tb_student where sno='".$_POST["sno"]."'",$conn);
if ($sqlstr1){
echo "<script>alert('删除成功');location='page.php';</script>";
}
else
echo "<?php echo rows[0];?>";
echo "删除失败";
}
?>
</body>
</html>