HPH-——>mysql 批量删除

批量删除

 

va<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<br />
<form action="main.php" method="post">
<div>姓名:
<input type="text" name="xm" /> 
<input type="submit" value="查询" />
</div>
</form>
<br />

<form action="piliangshanchu.php" method="post">
<table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td><input type="checkbox" onclick="quanxuan(this)" />代号</td>
        <td>姓名</td>
        <td>性别</td>
        <td>民族</td>
        <td>生日</td>
        <td>操作</td>
    </tr>
    
    <?php
    
    //先判断有没有提交值
    $xxm = "";
    $tj = " 1=1 ";
    if(!empty($_POST["xm"]) && $_POST["xm"]!="")
    {
        $xxm = $_POST["xm"];
        $tj = " name like '%{$xxm}%' ";
    }
    
    //造连接对象
    $db = new  MySQLi("localhost","root","123","mydb");
    //写SQL语句
    $sql = "select * from info where ".$tj;
    echo $sql;
    //执行SQL语句
    $result = $db->query($sql);
    //读数据
    $attr = $result->fetch_all();
    
    foreach($attr as $v)
    {
        echo "<tr>";
        
        $sex = $v[2]?"":"";
        
        //根据名族代号查询名族名称
        $name = NationName($v[3]);
        
        //替换关键字
        $newname = str_replace($xxm,"<mark>{$xxm}</mark>",$v[1]);
        
        echo "<td><input type='checkbox' value='{$v[0]}' name='sc[]' class='qx' />{$v[0]}</td><td>{$newname}</td><td>{$sex}</td><td>{$name}</td><td>{$v[4]}</td><td><a href='shanchu.php?c={$v[0]}' onclick=\"return confirm('确定删除么?')\">删除</a><a href='xiugai.php?c={$v[0]}'>修改</a></td>";
        
        /*foreach($v as $v1)
        {
            echo "<td>{$v1}</td>";
        }*/
        
        echo "</tr>";
    }
    
    //给一个民族代号,返回民族名称
    function NationName($code)
    {
        //造连接对象
        $db = new  MySQLi("localhost","root","123","mydb");
        //写SQL语句
        $sql = "select name from nation where code='{$code}'";
        //执行SQL语句
        $result = $db->query($sql);
        
        $attr = $result->fetch_row();
        
        return $attr[0];
    }
    
    ?>
    
    
</table>
<a href="add.php"><input type="button" value="添加数据" /></a>

<input type="submit" value="批量删除" onclick="return confirm('确定删除么')" />
</form>

<script type="text/javascript">

function quanxuan(a)
{
    //找到下面所有的复选框
    var ck =document.getElementsByClassName("qx");
    
    //遍历所有复选框,设置选中状态
    for(var i=0;i<ck.length;i++)
    {
        if(a.checked)
        {
            ck[i].setAttribute("checked","checked");
        }
        else
        {
            ck[i].removeAttribute("checked");
        }
    }
    
}
</script>

</body>
</html>



批量删除处理
<?php
$attr = array();
if(!empty($_POST["sc"]))
{
    $attr = $_POST["sc"];
}

$db = new  MySQLi("localhost","root","123","mydb");

/*foreach($attr as $v)
{
    $sql = "delete from info where code='{$v}'";
    
    $db->query($sql);
}
*/

$str = implode("','",$attr);

//echo $str;

$sql = "delete from info where code in('{$str}')";
$db->query($sql);

//delete from info where code in('p001','p002','p003')

 

posted on 2016-11-02 19:40  奔跑的葛根  阅读(176)  评论(0编辑  收藏  举报

导航