第一步:在資料庫中建立要操作的信息表 如下圖: 第二步:實現對該信息表中數據的刪除功能 代碼如下:main(主頁面) <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>刪除功能</title> <script src="bootst ...
第一步:在資料庫中建立要操作的信息表 如下圖:
第二步:實現對該信息表中數據的刪除功能
代碼如下: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();//查詢語句返回二維數組
}
}
}