首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP表大容量操作删除

PHP表大容量操作删除
EN

Stack Overflow用户
提问于 2013-01-24 18:43:18
回答 3查看 4.6K关注 0票数 0

我正在使用HTML表中的复选框,而我要做的就是使用一个大容量操作。就像有人检查两个或三个复选框并使用大容量删除操作一样,因此应该删除表中的所有选中项。

这是我的HTML代码

代码语言:javascript
复制
              <table id="datatable_example" class="responsive table table-striped table-bordered" style="width:100%;margin-bottom:0; ">
            <thead>
              <tr>
                <th style="width:0px; padding-right:0px;" class="no_sort"> <label class="checkbox ">
                    <input type="checkbox">
                  </label>
                </th>
                <th style="width:200px;" class="no_sort"> Institue </th>
                <th style="width:150px;" class="no_sort"> Education </th>
                <th style="width:300px;" class="no_sort"> Description </th>
                <th style="width:150px;" class="ue no_sort"> Started </th>
                <th style="width:150px;" class="ue no_sort"> Completion </th>
                <th class="ms no_sort "> Actions </th>
              </tr>
            </thead>
            <tbody>
              <?php echo $educations ?>
            </tbody>
          </table>

编辑:这是表的主体部分。因为我在PHP中工作,所以我做了这样的事情。

代码语言:javascript
复制
    $educations .= "<tr>
                <td><label class=\"checkbox\">
                    <input type=\"checkbox\">
                  </label></td>
                <td class=\"to_hide_phone\"> $institue_name</td>
                <td class=\"to_hide_phone\"> $education_name</td>
                    <td>$education_desc</td>
                <td>$education_started</td>
                <td>$education_ended</td>
                <td class=\"ms\">
                <div class=\"btn-group1\"> 
                <a href=\"education-edit.php?id=$education_id\" class=\"btn btn-small\" rel=\"tooltip\" data-placement=\"left\" data-original-title=\" edit \">
                <i class=\"gicon-edit\"></i></a> 
                <a class=\"btn btn-small\" rel=\"tooltip\" data-placement=\"top\" data-original-title=\"View\">
                <i class=\"gicon-eye-open\"></i>
                </a> 
                <a class=\"btn  btn-small\" rel=\"tooltip\" data-placement=\"bottom\" data-original-title=\"Remove\"><i class=\"gicon-remove \"></i></a> 
                </div>
                </td>
              </tr>";

如果我选择/检查两行并单击delete操作,那么这两行都应该被删除。我想知道如何执行这个动作。

对不起,如果我不清楚,我正在努力,但不知道如何实现它。

更新:这是我已经尝试过的,远离你的帮助。

HTML区域:

代码语言:javascript
复制
<form action="<?php $_PHP_SELF ?>" method="post">
        <div class="content top">
          <table id="datatable_example" class="responsive table table-striped table-bordered" style="width:100%;margin-bottom:0; ">
            <thead>
              <tr>
                <th style="width:0px; padding-right:0px;" class="no_sort"> <label class="checkbox ">
                    <input type="checkbox" class="chk_boxes">
                  </label>
                </th>
                <th style="width:200px;" class="no_sort"> Institue </th>
                <th style="width:150px;" class="no_sort"> Education </th>
                <th style="width:300px;" class="no_sort"> Description </th>
                <th style="width:150px;" class="ue no_sort"> Started </th>
                <th style="width:150px;" class="ue no_sort"> Completion </th>
                <th class="ms no_sort "> Actions </th>
              </tr>
            </thead>
            <tbody>
              <?php echo $educations ?>
            </tbody>
          </table>
          <div class="row-fluid control-group">
            <div class="pull-left span6 " action="#">
              <div>


                <div class="controls inline input-large pull-left">                    
                  <select name="bulkaction" data-placeholder="Bulk actions: " class="chzn-select " id="default-select">
                    <option value=""></option>
                    <option value="delete">Delete</option>
                  </select>
                </div>
                <button type="submit" name="submitbulkaction" class="btn btn-inverse inline">Apply</button></form>

车身内部区域:

代码语言:javascript
复制
    $educations .= "<tr>
                <td><label class=\"checkbox\">
                    <input type=\"checkbox\" class=\"chk_boxes1\" name=\"ids[]\" value=\"$education_id\">
                  </label></td>
                <td class=\"to_hide_phone\"> $institue_name</td>
                <td class=\"to_hide_phone\"> $education_name</td>
                    <td>$education_desc</td>
                <td>$education_started</td>
                <td>$education_ended</td>
                <td class=\"ms\">
                <div class=\"btn-group1\"> 
                <a href=\"education-edit.php?id=$education_id\" class=\"btn btn-small\" rel=\"tooltip\" data-placement=\"left\" data-original-title=\" edit \">
                <i class=\"gicon-edit\"></i></a> 
                <a class=\"btn btn-small\" rel=\"tooltip\" data-placement=\"top\" data-original-title=\"View\">
                <i class=\"gicon-eye-open\"></i>
                </a> 
                <a class=\"btn  btn-small\" rel=\"tooltip\" data-placement=\"bottom\" data-original-title=\"Remove\"><i class=\"gicon-remove \"></i></a> 
                </div>
                </td>
              </tr>";

当我按“应用”按钮时执行的部分:

代码语言:javascript
复制
    if(isset($_POST['submitbulkaction']))
{
    $ids = array();
    foreach ((array) $_POST['ids'] as $id) {
    // sanitize input
    $id = (int) $id;
    if ($id) $ids[] = $id;
}
// good idea to say how much row will be deleted to DB
    $limit = count($ids);
    if ($limit) {
    // can see the output
    $ids = join(',', $ids);
    mysql_query("DELETE * FROM cv_education WHERE id IN ($ids) LIMIT $limit");
}
}

现在我没有收到任何错误,但也没有从数据库表中删除数据。

我的桌子名是:

cv_education

你知道我做错了什么吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-24 20:23:44

您需要设置像<input type="checkbox" name="ids[]" value="<?=$data['id']?>">这样的复选框。

ids[]会给你一个数组。

然后您可以像这样处理每个复选框来收集将被删除的ids

代码语言:javascript
复制
$ids = array();
foreach ((array) $_POST['ids'] as $id) {
    // sanitize input
    $id = (int) $id;
    if ($id) $ids[] = $id;
}
// good idea to say how much row will be deleted to DB
$limit = count($ids);
if ($limit) {
    // can see the output
    $ids = join(',', $ids);
    mysql_query("DELETE FROM cv_education WHERE edu_id IN ($ids) LIMIT $limit");
}
票数 1
EN

Stack Overflow用户

发布于 2013-01-24 18:47:57

首先,将输入命名为

代码语言:javascript
复制
<input type="checkbox" name="selectedRows[]" value="<?php echo $row['id'] ?>">

然后,在posted php文件中处理它,如

代码语言:javascript
复制
var_dump($_POST['selectedRows'])

$_POST['selectedRows']将是一个数组,其中包含您选择的值。

如果要在sql查询中使用此数组,请使用内爆IN,如下所示

代码语言:javascript
复制
// filter id's first for security.
$ids = array_map('intval',$_POST['selectedRows']);

mysqli_query("DELETE * FROM table_name WHERE id IN (".implode(',',$ids).")");
票数 1
EN

Stack Overflow用户

发布于 2013-01-24 18:50:30

我需要看到生成$educations的代码,但是我的想法是:

您的复选框将如下所示:

<input type="checkbox" name="educations[]" value="<?php $row['id']?>" />

当你提交表格时,你会:

代码语言:javascript
复制
foreach($_POST['educations'] as $value){
    // query
    mysql_query("DELETE FROM tbl WHERE id = " . $value);
}

这只是一个指引。您可以使用sql注入。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14508460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档