6.12php對資料庫的刪除和批量刪除

weixin_30488085發表於2020-04-06

//主頁

<?php
$bt = empty($_POST['bt'])?'':$_POST['bt'];
$zz = empty($_POST['zz'])?'':$_POST['zz'];
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<!--引入bootstrap css檔案-->
<link rel="stylesheet" href="css/js/css/bootstrap.min.css"/>
<!--引入jQuery-->
<script src="css/js/jquery-3.3.1.min.js"></script>
<script src="css/js/bootstrap.min.js"></script>
<title>Document</title>
</head>
<body>
<form action="" method="post">
標題 :<input type="text" name="bt" id="" value="<?php echo $bt;?>" /><!--輸上值可以保留-->
作者 :<input type="text" name="zz" id="" value="<?php echo $zz;?>" />
<input type="submit" name="" id="" value="查詢"/>
</form>
<form action="piliang.php" method="post">
<table border="1px" cellspacing="" cellpadding=""class="table table-dark">
<tr>
<td><input type="checkbox" οnclick="quanxuan(this)"/></td>
<td>序號</td>
<td>標題</td>
<td>作者</td>
<td>來源</td>
<td>內容</td>
<td>釋出時間</td>
<td>修改</td>
<td>刪除</td>
</tr>
<?php
$severname = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "ceshi";
$conn = new mysqli($severname,$dbuser,$dbpassword,$dbname);
if($conn->connect_error){
die("連結失敗");
}
$sql = "select * from wz where biaoti like '%$bt%' and zuozhe like '%$zz%'";
//一進頁面兩個條件為空,所以還是查詢所有
$result = $conn->query($sql);
$attr = $result->fetch_all();
$str = "";
for($i=0;$i<count($attr);$i++){
$str.="<tr>";
$str.="<td><input type='checkbox' name='ck[]' id='' value='{$attr[$i][0]}' class='qx'/></td>";
//name='ck[]' 如果不加[]後面傳值沒法傳陣列,後面名字不用[];
for($j=0;$j<count($attr[$i]);$j++){
$str.="<td>{$attr[$i][$j]}</td>";
}
$str.="<td><a href='xiugai.php?bs={$attr[$i][0]}'>修改</a></td>";
$str.="<td><a href='shanchu.php?bs={$attr[$i][0]}'>刪除</a></td>";
$str.="</tr>";
}
echo $str;
?>
</table>
<input type="submit" value="批量刪除"/>
</form>
<a href="xinzeng.php">新增</a>
</body>
</html>
<script type="text/javascript">
function quanxuan(t) {
var aa = document.getElementsByClassName("qx");
for (var i=0;i<aa.length;i++) {
if (t.checked==true) {
aa[i].checked=true;
}else if(t.checked==false){
aa[i].checked=false;
}
//aa[i].checked = t.checked?true:false;
}

}
</script>

//刪除

<?php
$bs=$_GET['bs'];
$severname = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "ceshi";
$conn = new mysqli($severname,$dbuser,$dbpassword,$dbname);
$sql = "delete from wz where id ='$bs'";
$result = $conn->query($sql);
header("Location:0612.php");
?>

//批量刪除

<?php
$ckattr = $_POST['ck'];
$str = join("','",$ckattr);
echo($str);
$conn =new mysqli("localhost","root","","ceshi");
if($conn->connect_error){
die("連結失敗");
}
$sql = "delete from wz where id in ('{$str}')";
if($result = $conn->query($sql)){
header("Location:0612.php");
};
?>

轉載於:https://www.cnblogs.com/sunhao1987/p/9174943.html

相關文章