简体   繁体   中英

PHP table bulk action delete

I am using checkboxes in HTML table and what I am trying to do is using a bulk action. Like if someone checks two or three checkboxes and use the bulk delete action, so all checked items from the table should be deleted.

在此处输入图片说明

Here is my HTML code

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

EDIT: This is the Body Section of the Table. As I am working in PHP so I made it something like this.

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

I want to do that if I select/check two rows and I click the delete action then both rows should be deleted. I want to know how to perform this action.

Sorry if I am not being clear, I am trying but can't figure out how to implement it.


Update: Here what I have tried so far from your help.

HTML Area:

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

Inside tbody area:

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

The Part that executes when I press APPLY button:

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

Well Now I am not getting any error, but also data is not being deleted from database tables too.

My table name is:

cv_education

Any ideas what I am doing wrong?

first, name your input like

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

then, handle it in posted php file like

var_dump($_POST['selectedRows'])

$_POST['selectedRows'] will be an array, which contains values you selected.

If you want to use this array in your sql query, use implode and IN like

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

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

You need to set checkbox'es like <input type="checkbox" name="ids[]" value="<?=$data['id']?>"> .

ids[] will give you an array.

And then you can process each checkbox like this to collect ids that will be deleted;

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

I would need to see the code that produces $educations but the idea is this:

Your checkboxes would look like this:

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

And when you submit the form you would do:

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

This is just a guideline. You're open to sql injection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM