php批量删除操作(数据访问)

5年以前  |  阅读数:320 次  |  编程语言:PHP 

本文实例为大家分享了php批量删除操作的具体代码,供大家参考,具体内容如下

1.批量删除页面 piliangcaozuo.php


    <body>
    <form action="shanchu.php" method="post">
    <table width="100%" border="1" cellpadding="0" cellspacing="0">
      <tr>
        <td><input type="checkbox" name="qx" onclick="quanxuan(this)"/>代号</td>
        <td>名称</td>   
      </tr>
      <?php
       require"DBDA.class1.php";
       $db = new DBDA();
       $sql = "select * from nation";
       $arr = $db->query($sql);
       foreach($arr as $v)
      {
        echo "<tr>
            <td><input type='checkbox' name='ck[]' class='ck' value='{$v[0]}'/>{$v[0]}</td>
            <td>{$v[1]}</td>   
           </tr>";
      }
      ?>  
    </table>
    <input type="submit" value="批量删除" />
    </form>
    </body>
    <script type="text/javascript">
    function quanxuan(qx)
    {
      var ck=document.getElementsByClassName("ck");
      if(qx.checked)
      {
        for(var i=0;i<ck.length;i++)
        {
          ck[i].setAttribute("checked","checked");
        }
      }
      else
      {
        for(var i=0;i<ck.length;i++)
        {
          ck[i].removeAttribute("checked");
        }
      }
    }
    </script>
    </html>

引用的封装类 DBDA.class1.php


    <?php
    class DBDA
    {
      public $host = "localhost";
      public $uid = "root";
      public $pwd = "123";
      public $dbname = "test_123";
      //执行SQL语句返回相应的结果
      //$sql 要执行的SQL语句
      //$type 代表SQL语句的类型,0代表增删改,1代表查询
      function query($sql,$type=1)
      {
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);

        $result = $db->query($sql);

        if($type)
        {
          //如果是查询,显示数据
          return $result->fetch_all();
        }
        else
        {
          //如果是增删改,返回true或者false
          return $result;
        }
      }
    }

2.删除处理界面 sanchu.php


    <?php
    $arr = $_POST["ck"];

    require"DBDA.class.php";
    $db = new DBDA();
    //delete from nation where code in('n001','n002','n003')

    $str = implode("','",$arr); 
    $sql = "delete from nation where code in('{$str}')";
    /*echo $sql;*/
    if($db->query($sql,0))
    {
      header("location:piliangcaozuo.php");
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
将二进制数据转为16进制以便显示
获取IMSI
获取IMEI
Java生成UUID
PHP自定义函数获取搜索引擎来源关键字的方法
让你成为最历害的git提交人
在Zeus Web Server中安装PHP语言支持
指定应用ID以获取对应的应用名称
再谈PHP中单双引号的区别详解
Python 2与Python 3版本和编码的对比
php+ajax+json 详解及实例代码
Yii2汉字转拼音类的实例代码
php封装的page分页类完整实例
php数组合并array_merge()函数使用注意事项
PHP实现简单爬虫的方法
PHP设计模式之工厂模式与单例模式
php实现数组中索引关联数据转换成json对象的方法
wget使用技巧