thinkphp5數(shù)據(jù)庫怎么批量刪除
創(chuàng)新互聯(lián)建站是一家專業(yè)的成都網(wǎng)站建設(shè)公司,我們專注成都網(wǎng)站制作、做網(wǎng)站、網(wǎng)絡(luò)營(yíng)銷、企業(yè)網(wǎng)站建設(shè),外鏈,一元廣告為企業(yè)客戶提供一站式建站解決方案,能帶給客戶新的互聯(lián)網(wǎng)理念。從網(wǎng)站結(jié)構(gòu)的規(guī)劃UI設(shè)計(jì)到用戶體驗(yàn)提高,創(chuàng)新互聯(lián)力求做到盡善盡美。
// 以下方法供參考
$id = 3;
// 1、如果id是主鍵,且Mysql數(shù)據(jù)庫
$new -delete($id);
// 2、用表示篩選條件的數(shù)組$map
$map["id"]=$id;
$new -where($map)-delete();
// 3、如果id是主鍵
$new -delete($map);
// 4、直接用數(shù)組
$new -where(array("id"=$id))-delete();
// 5、刪除多條1,3,5,6,8
$ids = array(1,3,5,6,8);
$maps["id"] = array("in",$ids); // 或者 $maps["id"] = array("in","1,3,5,6,8");
$new -where($maps)-delete();
sample?code:
?php?
if($del_id!=""){?
$del_num=count($del_id);?
for($i=0;$i$del_num;$i++){?
mysql_query("Delete?from?news?where?id='$del_id[$i]'");?
}??
echo("script?type='text/javascript'alert('刪除成功!');history.back();/script");?
}else{?
echo("script?type='text/javascript'alert('請(qǐng)先選擇項(xiàng)目!');history.back();/script");?
}?
?
1.前臺(tái)
!DOCTYPE html
html
head
title批量刪除/title
/head
body
scripttype="text/javascript"
//復(fù)選框
function checkall(all)
{
var ck = document.getElementsByClassName("ck");
if(all.checked)
{
for(var i=0;ick.length;i++)
{
ck[i].setAttribute("checked","checked");
}
}
else
{
for(vari=0;ick.length;i++)
{
ck[i].removeAttribute("checked");
}
}
}
/script
formaction="test.php"method="post"
tableborder="1"
trthinputtype="checkbox"name="all"onclick="checkall(this)"/id/thth名字/th/tr
!-- 此處調(diào)用顯示列表函數(shù) --
?phpshow() ?
trtdcolspan="3"inputtype="submit"value="批量刪除"/td/tr
/table
/form
/body
?php
//顯示列表
function show()
{
//連接數(shù)據(jù)庫
@mysql_connect('localhost','root','');
mysql_select_db('test');
mysql_query('set names utf8');
$sql="select id,name from test";
$res=mysql_query($sql);
//循環(huán)取出數(shù)據(jù)
while($row=mysql_fetch_row($res))
{
echo "tr
td
inputtype='checkbox'value='{$row[0]}'name='item[]'class='ck'/
{$row[0]}
/td
td{$row[1]}/td
/tr";
}
}
?
/html
2.后臺(tái)
?php
//接收post傳來的數(shù)組
$arr=$_POST["item"];
/**
* 批量刪除
* 思路:把前臺(tái)批量選擇的數(shù)據(jù)放在數(shù)組里,刪除該數(shù)組即可
* @param $arr
* @return $res 成功or失敗
*/
functionbatch_del($arr)
{
@mysql_connect('localhost','root','');
mysql_select_db('test');
mysql_query('set names utf8');
//把數(shù)組元素組合為字符串:
$str= implode("','",$arr);
//in 表示多個(gè)
$sql="delete from test where id in('{$str}')";
$res= mysql_query($sql);
if(!$res){
echo"刪除失敗";
}else{
if(mysql_affected_rows()0){
echo"刪除成功";
}else{
echo"沒有行受到影響";
}
}
}
//調(diào)用批量刪除函數(shù)
batch_del($arr);
SCRIPT LANGUAGE="JavaScript"
!--
function quan()
{
if(document.getElementsByName("qx[]")[0].checked==false)
{
for(var i=0;idocument.getElementsByName("quanxuan[]").length;i++)
{
document.getElementsByName("quanxuan[]")[i].checked=false;
}
}
if(document.getElementsByName("qx[]")[0].checked)
{
for(var i=0;idocument.getElementsByName("quanxuan[]").length;i++)
{
document.getElementsByName("quanxuan[]")[i].checked=true;
}
}
}
//--
function xuan()
{
var a=new Array();
var i;
var c=0;
for(i=0;idocument.getElementsByName("quanxuan[]").length;i++)
{
if(document.getElementsByName("quanxuan[]")[i].checked)
{
a[c]=document.getElementsByName("quanxuan[]")[i].value;
c++;
/*alert(a);*/
}
}
if(a.length==0)
{
alert("請(qǐng)選擇");
}
else
{
var ss=confirm("確定刪除?");
if(ss==true)
{
location.href="shanchu.php?id="+a;
}
}
}
/SCRIPT
input type="checkbox" name="qx[]" onclick="quan();"font size="2"全選/font
a href="javascript:xuan()"選擇刪除/a
TABLE
TR
TD INPUT TYPE="checkbox" NAME="quanxuan[]" value="1"/TD
TDa/TD
/TR
TR
TD INPUT TYPE="checkbox" NAME="quanxuan[]" value="2"/TD
TDb/TD
/TR
TR
TD INPUT TYPE="checkbox" NAME="quanxuan[]" value="3"/TD
TDc/TD
/TR
TR
TD INPUT TYPE="checkbox" NAME="quanxuan[]" value="4"/TD
TDd/TD
/TR
TR
TD INPUT TYPE="checkbox" NAME="quanxuan[]" value="5"/TD
TDe/TD
/TR
/TABLE
傳值以后 在PHP頁 接值 $zhi=$_GET["a"]; 然后把 $zhi 拆分成數(shù)組
$zhi=explode(',',$zhi);
在循環(huán)刪除就可以
for($i=0;$icount($idd);$i++)
{
$con-query("delete from 表名 where ajbg_bh='$zhi[$i]'");
}
?php
//先構(gòu)造SQL語句
$SQL="DELETE?FROM?a?WHERE?b='123'";//刪除a表中所有b字段值為123的記錄
$ALL="DELETE?FROM?a";//刪除a表中所有記錄
$ALL2="TRUNCATE?a";//刪除a表中所有記錄,(推薦使用)
//使用mysql_query命令執(zhí)行SQL語句,如:
mysql_query($SQL);
問題很大,
首先,act==delAll是怎么來的?還有$id=implode('ID')這也是錯(cuò)誤的,$id的值是怎么來的?何況implode()函數(shù)是將字符串打散為數(shù)組,他有2個(gè)參數(shù),第一個(gè)是標(biāo)記符(也就是按照什么來打散數(shù)組),第二個(gè)是要打散的字符串。
其實(shí),你復(fù)選框有多個(gè),那就要用for循環(huán)依次獲取他的值,拼接成一個(gè)字符串。
給你一個(gè)我做是實(shí)例,你按照這個(gè)參考一下:
?php
include_once 'include/confing.php';//引入數(shù)據(jù)庫操作類
$alldel=$date-ps('alldel');//post方式獲得隱藏域alldel的值
if($date-is($alldel)){//如果alldel的值為真(依次來判斷表單是否提交,因?yàn)槲疫@php邏輯代碼和html是在同一個(gè)頁面,判斷一下有必要)
$ids='';//初始$ids的值為空
for($i=0;$icount($_POST['checks']);$i++){//根據(jù)頁面有多少個(gè)復(fù)選框進(jìn)行for循環(huán)
$s=$_POST['checks'][$i];//依次取得復(fù)選框的值
$ids.=$s.',';//拼接成字符串并在后面加上一個(gè)英文半角的逗號(hào)
}
$ids=substr($ids,0,strlen($ids)-1);//因?yàn)槠唇映鰜淼淖址詈髸?huì)多出一個(gè)逗號(hào),所以用substr函數(shù)將他去掉
$del_sql="Delete from websehzhi where tid in ($ids)";//構(gòu)建sql刪除語句
if($date-del_query($del_sql)){//如果sql語句執(zhí)行成功
$date-msmdurl('成功刪除選中記錄','?page='.$date-gt('page'));//成功提示并跳轉(zhuǎn)到指定url
}else{//否則
$date-msmdurl('刪除失敗,您未選中要?jiǎng)h除的記錄','?page='.$date-gt('page'//失敗提示并跳轉(zhuǎn)到指定url));
}
}
?
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
title批量刪除/title
/head
body
form method="post" name="form" id="form"
input type="checkbox" name="checks[]" value="35" /這是第十條數(shù)據(jù)br/
input type="checkbox" name="checks[]" value="34" /這是第九條數(shù)據(jù)br/
input type="checkbox" name="checks[]" value="33" /這是第八條數(shù)據(jù)br/
input type="checkbox" name="checks[]" value="32" /這是第七條數(shù)據(jù)br/
input type="checkbox" name="checks[]" value="31" /這是第六條數(shù)據(jù)br/
input type="checkbox" name="checks[]" value="30" /這是第五條數(shù)據(jù)br/
input type="checkbox" name="checks[]" value="29" /這是第四條數(shù)據(jù)br/
input type="checkbox" name="checks[]" value="28" /這是第三條數(shù)據(jù)br/
input type="checkbox" name="checks[]" value="27" /這是第二條數(shù)據(jù)br/
input type="checkbox" name="checks[]" value="26" /這是第一條數(shù)據(jù)br/
input type="hidden" name="alldel" value="1" /
button type="submit"批量刪除/button
/form
/body
/html
我這里是使用了面對(duì)對(duì)象,你不要管我的類對(duì)象代碼,反正原理都差不多!
你也可以使用面對(duì)過程,都差不多!
文章題目:php數(shù)據(jù)表批量刪除 mysql批量刪除表數(shù)據(jù)
轉(zhuǎn)載注明:http://muchs.cn/article40/hgsieo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、App開發(fā)、搜索引擎優(yōu)化、靜態(tài)網(wǎng)站、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容