PHP批量删除

$db = new MySQLi("localhost","root","123","mydb");
$sql = "insert into kemu values('','基础知识')";
$db->query($sql);
echo $db->insert_id; //取上一次添加数据的主键值 主键为自增加的情况

include("DBDA.class.php");//引用类
$db=new DBDA();

建立数据库操作封装类

class DBDA
{
public $host="localhost"; //服务器地址
public $uid="root"; //用户名
public $pwd="123"; //密码

public $dbconnect; //连接对象

//操作数据库的方法
//$sql代表需要执行的SQL语句
//$type代表SQL语句的类型,1代表查询,2代表增删改
//$dbname代表要操作的数据库名称
//如果是查询,返回二维数组
//如果是增删改,返回true或false
function Query($sql,$type=1,$dbname="mydb")
{
//造连接对象
$this->dbconnect = new MySQLi($this->host,$this->uid,$this->pwd,$dbname);
//判断是否出错
if(!mysqli_connect_error())
{
//如果连接成功,执行SQL语句
$result = $this->dbconnect->query($sql);

//根据语句类型判断
if($type==1)
{
//如果是查询语句,返回二维数组
return $result->fetch_all();
}
else
{
//如果是其他语句,返回true或false
return $result;
}
}
else
{
return "连接失败!";
}
}
}

<!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>
<h1>主页面</h1>
<form action="piliangshanchu.php" method="post">
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
	<td>代号</td>
    <td>姓名</td>
    <td>性别</td>
    <td>民族</td>
    <td>生日</td>
    <td>操作</td>
</tr>
<?php
//造对象
$db = new MySQLi("localhost","root","123","mydb");
//判断是否出错
!mysqli_connect_error() or die("连接失败!");
//写SQL语句
$sql = "select * from Info";
//执行SQL语句
$result = $db->query($sql);

//读取数据
$attr = $result->fetch_all();

//遍历数组,显示
foreach($attr as $v)
{
	
	$sex = $v[2]?"男":"女";
	
	$sql = "select Name from Nation where Code = '{$v[3]}'";
	$r = $db->query($sql);
	$a = $r->fetch_row();
	
	echo "
	<tr>
	<td>
	<input class='ck' name='ck[]' type='checkbox' value='{$v[0]}' />
	{$v[0]}</td>
    <td>{$v[1]}</td>
    <td>{$sex}</td>
    <td>{$a[0]}</td>
    <td>{$v[4]}</td>
	<td><a href='delete.php?code={$v[0]}'>删除</a><a href='update.php?code={$v[0]}'>修改</a></td>
</tr>
	";
}
?>
<tr>
<td><input type="checkbox" onclick="CheckAll(this)" />全选</td>
<td><input type="submit" value="批量删除" /></td>
</tr>
</table>
<a href="add.php">添加数据</a>
</form>
</body>
<script type="text/javascript">
function CheckAll(a)
{
	var ck = document.getElementsByClassName("ck");
	//a.checked;
	//document.getElementById().removeAttribute
	for(var i=0;i<ck.length;i++)
	{
		if(a.checked)
		{
			ck[i].setAttribute("checked","checked");
		}
		else
		{
			ck[i].removeAttribute("checked");
		}
	}
	
}
</script>
</html>

<?php
class DBDA
{
	public $host="localhost";	//服务器地址
	public $uid="root";	 		//用户名
	public $pwd="123"; 			//密码
	
	public $dbconnect; //连接对象
	
	//操作数据库的方法
	//$sql代表需要执行的SQL语句
	//$type代表SQL语句的类型,1代表查询,2代表增删改
	//$dbname代表要操作的数据库名称
	//如果是查询,返回二维数组
	//如果是增删改,返回true或false
	function Query($sql,$type=1,$dbname="mydb")
	{
		//造连接对象
		$this->dbconnect = new MySQLi($this->host,$this->uid,$this->pwd,$dbname);
		//判断是否出错
		if(!mysqli_connect_error())
		{
			//如果连接成功,执行SQL语句
			$result = $this->dbconnect->query($sql);
			
			//根据语句类型判断
			if($type==1)
			{
				//如果是查询语句,返回二维数组
				return $result->fetch_all();
			}
			else
			{
				//如果是其他语句,返回true或false
				return $result;
			}
		}
		else
		{
			return "连接失败!";
		}
	}
}

<?php
$ck = $_POST["ck"];
include("DBDA.class.php");
$db = new DBDA();

foreach($ck as $v)
{
	$sql = "delete from Info where Code='{$v}'";
	$db->Query($sql,0);
}

header("location:main.php");

  

原文地址:https://www.cnblogs.com/hamilton/p/5587914.html