Sizzling Code Sizzling Code - 1 month ago 6
PHP Question

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.

enter image description here

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?

Answer

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