Delete multiple rows – PHP – Mysql

Delete multiple rows from php mysql with checkbox, Delete multiple rows by selecting checkboxes using PHP, php – How to delete multiple rows from mysql with checkbox, Delete multiple rows using PHP, Delete multiple rows in mysql with check box, How to delete multiple row using checkbox, Delete multiple records using checkbox in php, Delete multiple rows php mysql using checkbox.

Delete multiple rows with checkbox option. Example given for deletes multiple rows with Ajax, PHP and MySQL. For managing table entries we need to give delete option with every row. Feature to select all rows for delete with one click is better option for user/admin.

In below example you can delete selected or all record by checkbox delete option.

Example code:
Example divided in two files
file 1: list.php

  • HTML and PHP code for display table rows
  • jQuery code for get selected checkbox and make ajax request

file 2: delete.php

  • PHP code for delete rows

Delete multiple rows from php mysql example file 1:

HTML and PHP code for display table rows: list.php

<table>
<tr>
<th>ID</th>
...
</tr>
<?php while(//FETCH ROW CODE){ ?>
<tr>
<td>
<input type="checkbox" class="deleteMe" id="row_<?php echo $row->id; ?>" value="<?php echo $row->id; ?>">
<label for="row_<?php echo $row->id; ?>"> <?php echo $row->id; ?></label>
</td>
...
</tr>
<?php } ?>
<tr>
<th>
<input type="checkbox" id="checkAll">
<label for="checkAll">Check all</label>
</th>
...
</tr>
<tr>
<th><button id="deleteAllBtn">Delete</button></th>
...
</tr>
</table>

Delete multiple rows from php mysql example js code:

jQuery code for get selected checkbox and make ajax request:

$(function(){
$('#checkAll').change(function(){
    $('.deleteMe').prop('checked', $(this).is(':checked'));
});

$('.deleteMe').change(function(){
    if(!$(this).is(':checked'))
        $('#checkAll').prop('checked', false);
});

$('#deleteAllBtn').click(function(){
    var idArr = [];
    var strId = '';
    $('.deleteMe:checked').each(function(){
        idArr.push($(this).val());
    });
 
    if(idArr.length){
        strId = idArr.join(',');
  
        $.post('delete.php', {'opt' : 'delete', 'ids' : strId})
        .done(function(data){
            if(data.success){
                //Process on successfully delete
            }else{
                //Process on not successfully delete
                alert(data.message);
            }
        })
    }
});
});

Delete multiple rows from php mysql example file 2:

PHP code for delete rows: delete.php

if(isset($_POST['opt']) && $_POST['opt'] = 'delete')
{
    $retArr = array();
    $retArr['success'] = false;
    $retArr['message'] = 'Record/s not deleted.';
    
    if(isset($_POST['ids']))
    {
        //SQL CONNECTION ($dbh)

        $sql = 'DELETE FROM tableName WHERE id IN('.$_POST['ids'].')';
  
        $result = $dbh->exec($sql);
  
        if($result)
        {
            $retArr['success'] = true;
            $retArr['message'] = 'Record/s deleted.';
        }
    }
 
    header('Content-type: application/json;');
    echo json_encode($retArr);
    exit;
}

Delete multiple rows from php mysql working demo:


 ID