Sunday, November 11, 2018

Multiple delete using checkbox PHP MySql

<?php
include("./connect.php");
$sql="select * from tblPerson where 1";
$res=mysqli_query($con,$sql);
if(isset($_POST['btnDel'])){
echo "delete";
$id=$_POST['chk'];
$idList=implode(",",$id);
echo $idList;
$sql="delete from tblPerson where pid in(".$idList.")";
$res=mysqli_query($con,$sql);
header("Location:./disp.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>
</head>

<body>
<form method="post">
<table border="1" width="779" cellpadding="10" cellspacing="0">
<tr>
<th colspan="9" align="right"><a href="ins_reg.php">Add New</a></th>
</tr>
<tr>
<th>Name</th>
    <th>Gender</th>
    <th>Contact No.</th>
    <th>Hobbies</th>
    <th>Address</th>
    <th>Designation</th>
    <th>Edit</th>
    <th>Delete</th>
    <th>Multiple Delete</th>
</tr>
<?php
while($row=mysqli_fetch_array($res)){
?>
<tr align="center">
    <td><?php echo $row['pname']; ?></td>
        <td><?php echo $row['gender']; ?></td>
        <td><?php echo $row['contactNo']; ?></td>
        <td><?php echo $row['hobbies']; ?></td>
        <td><?php echo $row['address']; ?></td>
        <td><?php echo $row['designation']; ?></td>
        <td><a href="ins_reg.php?id=<?php echo $row[0]; ?>&act=edit">Edit</a></td>
        <td><a href="disp.php?id=<?php echo $row[0]; ?>&act=del">Delete</a></td>
        <td><input type="checkbox" name="chk[]" value="<?php echo $row[0]; ?>"/></td>
    </tr>
<?php
}
?>
<tr>
<td colspan="9" align="right"><input type="submit" name="btnDel" value="Delete Marked"/></td>
</tr>
</table>
</form>
<?php
if(isset($_GET['act'])){

if(strcmp($_GET['act'],"del")==0){

$sql="delete from tblPerson where pid=".$_GET['id'];
$res=mysqli_query($con,$sql);
header("Location:./disp.php");
}
}
?>
</body>
</html>

No comments:

Post a Comment