【问题标题】:PHP table bulk action deletePHP 表批量操作删除
【发布时间】:2013-01-08 15:05:54
【问题描述】:

我在 HTML 表格中使用复选框,我想要做的是使用批量操作。 就像某人选中了两个或三个复选框并使用批量删除操作一样,那么表中所有选中的项目都应该被删除。

这是我的 HTML 代码

              <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 中工作,所以我做了这样的事情。

    $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>";

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

对不起,如果我不清楚,我正在尝试但无法弄清楚如何实现它。


更新: 这是我迄今为止在您的帮助下所做的尝试。

HTML 区域:

<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>

主体区域内:

    $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>";

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

    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

任何想法我做错了什么?

【问题讨论】:

  • 你能分享你的 bootsrap css 文件吗?

标签: php arrays checkbox html-table


【解决方案1】:

首先,将您的输入命名为

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

然后,在发布的 php 文件中处理它,如

var_dump($_POST['selectedRows'])

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

如果你想在你的sql查询中使用这个数组,使用implodeINlike

// filter id's first for security.
$ids = array_map('intval',$_POST['selectedRows']);

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

【讨论】:

  • array_map 应该颠倒过来,先回调。 $ids = array_map('intval',$_POST['selectedRows']);
【解决方案2】:

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

您的复选框将如下所示:

&lt;input type="checkbox" name="educations[]" value="&lt;?php $row['id']?&gt;" /&gt;

当您提交表单时,您会这样做:

foreach($_POST['educations'] as $value){
    // query
    mysql_query("DELETE FROM tbl WHERE id = " . $value);
}

这只是一个指导方针。您对 sql 注入持开放态度。

【讨论】:

  • 所以你的意思是sql注入不安全?那么有没有更安全的方法呢?
  • 只需escape您发布的数据。
【解决方案3】:

您需要设置复选框,例如&lt;input type="checkbox" name="ids[]" value="&lt;?=$data['id']?&gt;"&gt;

ids[] 会给你一个数组。

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

$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");
}

【讨论】:

  • 谢谢你 4 的回复,先生,我是 PHP 的一周。 ids[] 应该是空的吗?还是我需要在“[]”大括号之间加上值?
  • 不,你不会在大括号之间放任何东西,只需在写出它们时用来自“db”的row id 填充input value(所以你在图中显示)
  • 我认为是$education_id,可以这样写; value="&lt;?=$education_id?&gt;"
  • 好的。先生,我做到了,但我在 foreach 的过程中遇到了错误。它说无效参数“警告:在第 11 行的 C:\xampp\htdocs\projects\pakistanihaider\admin\education.php 中为 foreach() 提供的参数无效”怎么办?
  • 好的,看看更新(即使它是空的也可以转换;foreach ((array) $_POST['ids'] as $id)
猜你喜欢
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-27
  • 2016-03-15
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
相关资源
最近更新 更多