案例:批量删除

一点知识:

1、php访问数据库,增删改查数据库,在页面显示出结果=数据库的结果

2、<radio><checkbox><option>标签,name="xx[]",表示选中多少提交多少,以数组形式提交,就得以数组形式获取。

value值是自己定义的,不同<text>的value。

通过name提交获取到的是value值。

批量删除:

点击按钮实现批量删除,需要一个form表单,需要一个批量处理页面(也可以放同一页面,但是还需加代码,实现初始效果).

主页面代码:

<form action="plsc.php" method="post">
<?php
$db=new MySQLi("localhost","root","123","info");
!mysqli_connect_error() or die("连接失败");
$sql="select * from info";
$result=$db->query($sql);
$attr=$result->fetch_all();
foreach($attr as $v)
{    $s=$v[2]==1?'男':'女';
    
    $sq="select name from nation where code='{$v[3]}'";
    $r=$db->query($sq);
    $a=$r->fetch_row();
    
    echo "<tr>
    <td align='center'><input type='checkbox' name='xx[]' value='$v[0]' class='a'/>{$v[0]}</td>
    <td align='center'>{$v[1]}</td>
    <td align='center'>{$s}</td>
    <td align='center'>{$a[0]}</td>
    <td align='center'>{$v[4]}</td>
    <td align='center'><a href='delete.php?code={$v[0]}'>删除</a>
    <a href='update.php?code={$v[0]}'>修改</a>
    </td>
    </tr>";
}
?>
<tr>
<td align="center"><input type="checkbox" onclick="checkall(this)"/>全选</td>
</tr>
<tr>
<td align="center"><input type="submit" value="批量删除"/></td>
</tr>
</form>

处理页面:

<?php
header("content-type:text/html;charset=utf-8");
if(!empty($_POST))
{
    $xx=$_POST["xx"];//一维数组
    var_dump($xx);
    //操作数据库删除
    $db=new MySQLi("localhost","root","123","info");
    !mysqli_connect_error() or die("连接失败");
    //选哪条删那条
    /*foreach($xx as $v)
        {
            $sql="delete from info where code='{$v}'";
            $result=$db->query($sql,0);
        }*/
    for($i=0;$i<=count($xx);$i++)
        {
            $sql="delete from info where code='{$xx[$i]}'";
            var_dump($sql);
            $result=$db->query($sql);
            var_dump($result);
        }
    
    if($result)
        {
            header("location:main.php");
        }
    else
        {
            echo "批量删除失败";
        }
}
原文地址:https://www.cnblogs.com/jinshui/p/5598102.html