服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - php批量删除操作(数据访问)

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

2021-05-21 16:03ChrissZhao PHP教程

这篇文章主要为大家详细介绍了php批量删除操作,批量删除页面和.除处理界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

1.批量删除页面 piliangcaozuo.php

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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");
}

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

延伸 · 阅读

精彩推荐