PHP : Delete Multiple Rows using Checkbox

Delete Multiple Rows using Checkbox


c1


● Create a PHP file to list record with check box ! here it is “delCheckBoxCL.php”.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="bootstrap-3.3.5-dist/css/bootstrap.min.css"/>
<script type="text/javascript">
function deleteConfirm(){
    var result = confirm("Are you sure to delete?");
    if(result){
        return true;
    }else{
        return false;
    }
}

$(document).ready(function(){
    $('#select_all').on('click',function(){
        if(this.checked){
            $('.checkbox').each(function(){
                this.checked = true;
            });
        }else{
             $('.checkbox').each(function(){
                this.checked = false;
            });
        }
    });
    
    $('.checkbox').on('click',function(){
        if($('.checkbox:checked').length == $('.checkbox').length){
            $('#select_all').prop('checked',true);
        }else{
            $('#select_all').prop('checked',false);
        }
    });
});
</script>
</head>

<body>
  <?php
  $host="127.0.0.1";
  $user="root";
  $pass="";
  $dbname="mmtu";
  $db= new PDO("mysql::host=$host;dbname=$dbname",$user,$pass);
  $sql="select * from employee";
  $result=$db->query($sql);
  $result->setfetchmode(PDO::FETCH_ASSOC);
?>
    
<form name="form" action="delCheckBoxSR.php" method="post" onsubmit="return deleteConfirm();"/>
  
    <table class="table table-hover"  style="width:60%; margin:auto">
        <thead>
        <tr>
            <th>Select</th> 
            <th>code</th>
            <th>name</th>
            <th>department</th>
            <th>salary</th>
        </tr>
        </thead>
        <?php
           while( $row=$result->fetch())
		   {
        ?>
        <tr>
            <td align="center"><input type="checkbox" name="checked_id[]" class="checkbox" value="<?php echo $row['eid']; ?>"/></td>        
            <td><?php echo $row['eid']; ?></td>
            <td><?php echo $row['ename']; ?></td>
            <td><?php echo $row['dept']; ?></td>
            <td align="right">&#8377;<?php echo $row['salary']; ?></td>
        </tr> 
        <?php 
		 }
		$db=null;
		?>
        <tr>
        	<td align="left"> <strong>Select All <input type="checkbox" name="select_all" id="select_all" value=""/> </strong> </td>
            
            <td colspan="4" align="center">  <input type="submit" class="btn btn-danger" name="bulk_delete_submit" value="Delete"/> </td>
        </tr>
        
    </table>

</form>
</body>
</html>

c2


● Create another PHP file to take action for delete ! here it is “delCheckBoxSR.php”.

<?php
  
  $host="127.0.0.1";
  $user="root";
  $pass="";
  $dbname="mmtu";
  $db= new PDO("mysql::host=$host;dbname=$dbname",$user,$pass);
    if(isset($_POST['bulk_delete_submit'])){
        $idArr = $_POST['checked_id'];
        foreach($idArr as $id){
    		$db->exec("delete  from  employee  where eid=".$id);
        }
       
        header("Location:delCheckBoxCL.php");
    }
?>


 

c3


You may delete all record also.

c4

Posted in PHP