>  기사  >  백엔드 개발  >  PHP에서 일괄 삭제 작업을 구현하는 방법

PHP에서 일괄 삭제 작업을 구현하는 방법

墨辰丷
墨辰丷원래의
2018-05-22 11:21:553848검색

이 글에서는 주로 PHP 일괄 삭제 작업, 일괄 삭제 페이지 및 .delete 처리 인터페이스를 자세히 소개합니다. 관심 있는 친구는 참고할 수 있습니다.

구체적인 내용은 다음과 같습니다

1 .Batch 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=&#39;checkbox&#39; name=&#39;ck[]&#39; class=&#39;ck&#39; value=&#39;{$v[0]}&#39;/>{$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(&#39;n001&#39;,&#39;n002&#39;,&#39;n003&#39;)

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

관련 권장 사항:

ThinkPHP 구현 사용자 정보 조회, 업데이트 및 삭제 기능

mysql을 사용하여 루트 일반 사용자를 생성하고 삭제 기능자세한 설명

jQuery에서는 사용자 정보 테이블의 추가 및 삭제 기능

을 구현합니다.

위 내용은 PHP에서 일괄 삭제 작업을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.