首先是建一个主页代码,文件名userlist.php,下面是代码
<?php
$page =empty($_GET['page']) ? 1:$_GET['page'];
$link = mysqli_connect('localhost','root','200801');
if(!$link){
exit('数据库链接失败');
}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'zhigaole');
//---------------分页----------------
//求总条数
$sql = "select count(*) as count from user";
$result = mysqli_query($link , $sql);
$pageRes = mysqli_fetch_assoc($result);
$count = $pageRes['count'];
//每页显示数 每页显示五条
$num = 5;
//根据每页显示数可以求出来总页数
$pageCount = ceil($count/$num); //ceil取整
//var_dump($pageCount);
//根据总页数求出偏移量
$offset = ($page-1)*$num;
//---------------分页----------------
$sql = "select * from user limit ". $offset .','.$num;
$obj = mysqli_query($link ,$sql);
echo '<table width ="800" border = "1">';
echo '<th>编号</th><th>用户名</th><th>密码</th><th>物品编码</th><th>数量</th>
<th>性别</th><th>年龄</th><th>地址</th><th>状态</th><th>操作</th>';
while($rows = mysqli_fetch_assoc($obj)){
echo '<tr>';
echo '<td>'.$rows['id'].'</td>';
echo '<td>'.$rows['username'].'</td>';
echo '<td>'.$rows['password'].'</td>';
echo '<td>'.$rows['gid'].'</td>';
echo '<td>'.$rows['number'].'</td>';
echo '<td>'.($rows['sex']==1 ? '男' : '女').'</td>';
echo '<td>'.$rows['age'].'</td>';
echo '<td>'.$rows['live'].'</td>';
echo '<td>'.$rows['conidition'].'</td>';
echo '<td><a href = "del.php?id='.$rows['id'].'">删除</a>/<a href ="update.php?id='.$rows['id'].'">修改</a></td>';
echo '</tr>';
}
echo '</table>';
echo '<a href ="add.php">添加</a>';
$next = $page + 1;
$prev = $page - 1;
if ($next>$pageCount){
$next = $pageCount;
}
if ($prev<1){
$prev = 1;
}
mysqli_close($link);
?>
<a href ="userlist.php?page=1">首页</a>
<a href ="userlist.php?page=<?php echo $next;?>">下一页</a>
<a href ="userlist.php?page=<?php echo $prev;?>">上一页</a>
<a href ="userlist.php?page=<?php echo $pageCount;?>">尾页</a>
添加数据代码 文件名add.php 代码内容
<html>
<head>
<title>
</title>
<meta charset ="utf-8"/>
</head>
<body>
<form action = "doadd.php">
编 号:<input type ="text" value ="" name = "id" /> <br/>
用 户 名:<input type ="text" value ="" name = "username" /> <br/>
密 码:<input type ="text" value ="" name = "password" /> <br/>
物品编码:<input type ="text" value ="" name = "gid" /> <br/>
数 量:<input type ="text" value ="" name = "number" /> <br/>
性 别:<input type ="radio" name = "sex" value ="1" /> 男
<input type ="radio" name = "sex" value ="0" /> 女<br/>
年 龄:<input type ="text" value ="" name = "age" /> <br/>
地 址:<input type ="text" value ="" name = "live" /> <br/>
状 态:<input type ="text" value ="" name = "conidition" /> <br/>
<input type="submit" value="提交"/>
</form>
</body>
<html>
实施添加操作 文件名doadd.php 代码如下
<?php
$id =$_GET['id'];
$username =$_GET['username'];
$password =($_GET['password']);
$gid =$_GET['gid'];
$number =$_GET['number'];
$sex =$_GET['sex'];
$age =$_GET['age'];
$live =$_GET['live'];
$conidition =$_GET['conidition'];
var_dump($_GET);
$link = mysqli_connect('localhost','root','200801');
if(!$link){
exit('数据库链接失败');
}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'zhigaole');
$sql = "insert into user(id,username,password,gid,number,sex,age,live,conidition) values($id,'$username',$password,$gid,$number,$sex,$age,'$live','$conidition')";
$result = mysqli_query($link ,$sql);
if($result){
echo '添加成功<a href ="userlist.php">返回</a>';
}else{
echo '添加失败';
}
mysqli_close($link);
?>
删除操作,文件名 del.php 代码如下
<?php
$id = $_GET['id'];
$link = mysqli_connect('localhost','root','200801');
if(!$link){
exit('数据库链接失败');
}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'zhigaole');
$sql = "delete from user where id =$id";
$boolean = mysqli_query($link ,$sql);
if ($boolean && mysqli_affected_rows($link)){
echo '删除成功<a href = "userlist.php">返回</a>';
}else{
echo '删除失败';
}
mysqli_close($link);
?>
修改操作界面 文件名update.php 代码如下
<?php
$id = $_GET['id'];
$link = mysqli_connect('localhost','root','200801');
if(!$link){
exit('数据库链接失败');
}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'zhigaole');
$sql = "select * from user where id =$id";
$obj =mysqli_query($link ,$sql);
$rows = mysqli_fetch_assoc($obj);
?>
<html>
<form action = "diary.php">
<input type = "hidden" value = "<?php echo $id;?>" name = "id"/>
用 户 名:<input type ="text" value ="<?php echo $rows['username'];?>" name = "username" /> <br/>
密 码:<input type ="text" value ="<?php echo $rows['password'];?>" name = "password" /> <br/>
物品编码:<input type ="text" value ="<?php echo $rows['gid'];?>" name = "gid" /> <br/>
数 量:<input type ="text" value ="<?php echo $rows['number'];?>" name = "number" /> <br/>
性 别:<input type ="text" value ="<?php echo $rows['sex'];?>" name = "sex" /> <br/>
年 龄:<input type ="text" value ="<?php echo $rows['age'];?>" name = "age" /> <br/>
地 址:<input type ="text" value ="<?php echo $rows['live'];?>" name = "live" /> <br/>
状 态:<input type ="text" value ="<?php echo $rows['conidition'];?>" name = "conidition" /> <br/>
<input type = "submit" value = "执行修改"/>
</form>
</html>
修改操作执行代码 文件名diary.php 代码如下
<?php
var_dump($_GET);
$id =$_GET['id'];
$username =$_GET['username'];
$password =$_GET['password'];
$gid =$_GET['gid'];
$number =$_GET['number'];
$sex =$_GET['sex'];
$age =$_GET['age'];
$live =$_GET['live'];
$conidition =$_GET['conidition'];
$link = mysqli_connect('localhost','root','200801');
if(!$link){
exit('数据库链接失败');
}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'zhigaole');
$sql = "update user set username = '$username' ,password ='$password',gid ='$gid',number ='$number',sex = '$sex',age = '$age',live ='$live',conidition ='$conidition' where id = $id";
$result = mysqli_query($link ,$sql);
if($result && mysqli_affected_rows($link)){
echo '修改成功<a href = "userlist.php">返回</a>';
}else{
echo '修改失败';
}
mysqli_close($link);
?>