> 官網手冊 http://www.hmoore.net/manual/thinkphp5_1/354003
## 官方推薦寫法如下
~~~
// 根據主鍵刪除
Db::table('think_user')->delete([1,2,3]);
~~~
> delete 方法返回影響數據的條數,沒有刪除返回 0
## 刪除數據完整示例
批量刪除功能的原理是前端把需要刪除的數據post給后端,后端進行刪除操作,共兩部分

~~~
//批量刪除
$(".select_del").click(function(){
var url = "{:url('selectDel')}";
delSelect(url);
})
//批量刪除
function delSelect(url){
if (!getCheckboxNum()){
swal(
'請先選擇要刪除的項目',
'',
'error'
)
}else{
swal({
title: '確定刪除?',
text: "刪除后將無法恢復!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3c8dbc',
cancelButtonColor: '#d33',
confirmButtonText: '確定',
cancelButtonText: '取消'
}).then(function(isConfirm) {
if (isConfirm) {
//執行刪除操作
var id = $("input:checkbox[name='key[]']:checked").map(function(index,elem) {
return $(elem).val();
}).get().join(',');
//ajax
$.post(url,{id:id},function(result){
if(result.code == 1){
swal(result.msg,'','success').then(function(){window.location.reload()});
}else{
swal(result.msg,'','error');
}
});
//ajax
}
})
}
}
//獲取Checkbox被選擇個數
function getCheckboxNum(){
var checkbox = document.getElementsByName("key[]");
var j = 0; // 用戶選中的選項個數
for(var i=0;i<checkbox.length;i++){
if(checkbox[i].checked){
j++;
}
}
return j;
}
~~~
~~~
//批量刪除
public function selectDel(){
if(Request::isPost()) {
$id = input('post.id');
if (empty($id)) {
$result['status'] = 0;
$result['msg'] = 'ID不存在!';
return $result;
}
db('link')->delete($id);
return $result = ['code'=>1,'msg'=>'刪除成功!'];
}
}
~~~
- 基礎知識
- 查詢數據
- 添加數據
- 更新數據
- 刪除數據
- 批量刪除
- 編輯器的使用
- wangEditor的使用
- UEditor的使用
- ckeditor5的使用
- 圖片上傳
- 單圖上傳
- 多圖上傳
- 支付方式
- paypal
- paypal新版接入
- Excel導入導出
- PHPExcel 導出
- 標簽擴展
- 閉合標簽
- 非閉合標簽
- 郵件發送
- 阿里云短信發送
- 物流查詢
- 合成海報
- TP6.0根據IP獲取國家代碼
- TP6.0集成騰訊云短信
- TP6.0緩存的應用
- TP6.0集成JSON Web(JWT)
- TP6.0集成apiDoc 快速完成接口文檔
- TP6.0集成pjax
- 微信支付apiv3的異步通知如何驗證和接收
- PHP簽名認證
- TP6.0獲取最新省市縣三級數據并入庫
- PHP指定版本運行Composer
- linux端口占用查詢與處理