第一步:在数据库中建立要操作的信息表 如下图:
第二步:实现对该信息表中数据的删除功能
代码如下:main(主页面)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>删除功能</title>
<script src="bootstrap/js/jquery-1.11.2.min.js"></script> //引入这里的三个文件
<script src="bootstrap/js/bootstrap.min.js"></script>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div style="height: 100px;"></div>
<form action="batch_process.php" method="post"> //利用form表单进行提交页面
<table class="table table-hover" style="max-width: 800px;margin-left: 260px;">
<thead>
<tr>
<th><input type="checkbox" onclick="qx(this)"/> 代号</th>
<th>名称</th>
<th>价格</th>
<th>产地</th>
<th>库存</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php
$db = new MYSQLi("localhost","root","","0710_info");
$sql = "select * from fruit";
$result = $db->query($sql);
$arr = $result->fetch_all();
foreach($arr as $v){
echo "<tr>
<td><input type=‘checkbox‘ class=‘ck‘ value=‘{$v[0]}‘ name=‘sub[ ]‘/> {$v[0]}</td>
<td>{$v[1]}</td>
<td>{$v[2]}</td>
<td>{$v[3]}</td>
<td>{$v[4]}</td>
<td>
<a href=‘del_processpage.php?code={$v[0]}‘ onclick=\"return confirm(‘确定删除吗?‘)\"> //防止失误操作处理
<button type=‘button‘ class=‘btn btn-primary btn-xs‘>删除</button>
</a>
<a href=‘update_page.php?code={$v[0]}‘ onclick=\"return confirm(‘确定修改吗?‘)\">
<button type=‘button‘ class=‘btn btn-primary btn-xs‘>修改</button>
</a>
</td>
</tr>";
}
?>
</tbody>
</table>
<button type="submit" class="btn btn-danger btn-xs" style="margin-left: 260px;">批量删除</button>
</form>
</body>
//这里的JS操作为第四步批量删除的批量选择按钮的点击事件操作(同第四步)
<script>
function qx(qx){
var ck = document.getElementsByClassName("ck");
for(var i=0;i<ck.length;i++){
ck[i].checked=qx.checked;
}
}
</script>
</html>
delete(删除处理页面)
<?php
$code = $_GET["code"];
$db = new MYSQLi("localhost","root","","0710_info");
$sql = "delete from fruit where ids=‘{$code}‘";
if($db->query($sql)){
header("location:del_page.php");
}else{
echo "删除失败!";
}
第三步:实现对数据库中数据的修改功能(与主界面连接)
代码如下 :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改功能</title>
<script src="bootstrap/js/jquery-1.11.2.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
</head>
<style>
*{
margin: 0px auto;
padding: ;
}
.input-group{
margin-top: 15px;
}
.sub{
margin: 20px 260px 10px;
}
</style>
<body>
<div style="height: 100px;"></div>
<?php
$code = $_GET["code"];
$db = new MYSQLi("localhost","root","","0710_info");
$sql = "select * from fruit where ids=‘{$code}‘";
$result = $db->query($sql);
$arr = $result->fetch_row();
?>
<form action="update_processpage.php" method="post">
<div class="panel panel-default" style="max-width: 600px;">
<div class="panel-heading">
修改数据
</div>
<div class="panel-body">
<div class="input-group">
<span class="input-group-addon">代号</span>
<input type="text" class="form-control" readonly="readonly" placeholder="请输入代号" name="code" value="<?php echo $arr[0] ?>">
</div>
<div class="input-group">
<span class="input-group-addon">名称</span>
<input type="text" class="form-control" placeholder="请输入名称" name="name" value="<?php echo $arr[1] ?>">
</div>
<div class="input-group">
<span class="input-group-addon">价格</span>
<input type="text" class="form-control" placeholder="请输入价格" name="price" value="<?php echo $arr[2] ?>">
</div>
<div class="input-group">
<span class="input-group-addon">产地</span>
<input type="text" class="form-control" placeholder="请输入产地" name="chandi" value="<?php echo $arr[3] ?>">
</div>
<div class="input-group">
<span class="input-group-addon">库存</span>
<input type="text" class="form-control" placeholder="请输入库存" name="kucun" value="<?php echo $arr[4] ?>">
</div>
<button type="submit" class="btn btn-primary sub">提交</button>
</div>
</div>
</form>
</body>
</html>
update(修改处理页面)
<?php
$code = $_POST["code"];
$name= $_POST["name"];
$price = $_POST["price"];
$chandi = $_POST["chandi"];
$kucun = $_POST["kucun"];
$db = new MYSQLi("localhost","root","","0710_info");
$sql = "update fruit set
name=‘{$name}‘,price={$price},source=‘{$chandi}‘,numbers={$kucun} where ids=‘{$code}‘";
if($db->query($sql)){
header("location:del_page.php");
}else{
echo "修改失败!";
}
第四步:实现对数据库中数据的批量选择与删除的功能(与主界面连接)
JS操作代码如下:
<script>
function qx(qx){
var ck = document.getElementsByClassName("ck");
for(var i=0;i<ck.length;i++){
ck[i].checked=qx.checked;
}
}
</script>
PHP操作代码如下:
<?php
$arr= $_POST["sub"];
require_once "./DBDA.class.php";//加载类进入操作界面
$db = new DBDA();
$str = implode("‘,‘", $arr);
$sql = "delete from fruit where ids in (‘{$str}‘)";
if($db->query($sql,1)){
header("location:del_page.php");
}else{
echo "删除失败!";
}
***这里进行了数据访问类的封装操作(优化使用)
PHP代码如下:
<?php
class DBDA{
public $host="localhost";
public $uid="root";
public $pwd="";
public $dbname="0710_info";
/*
query方法:执行用户给的sql语句,并返回相应的结果
$sql:用户需要执行的sql语句
$type:用户需要执行的sql语句的类型
return:如果是增删语句改返回true或false,如果是查询语句返回二维数组
*/
public function query($sql,$type=1){//默认true为增删改
$db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
if(mysqli_connect_error()){
return "连接失败!";
}
$result = $db->query($sql);
if($type==1){
return $result;//增删改语句返回true或false
}else{
return $result->fetch_all();//查询语句返回二维数组
}
}
}