I'm trying to develop an admin panel. I need to delete employs from list of employs.. I have tried with this code. but i couldn't do it..
Error:
Undefined index checkbox.. (resolved this by using isset) But i couldn't delete data from database.
<?php
include "dbConnect.php";
$display= mysqli_query($con, "SELECT emplys.emp_id,emplys.emp_name,emplys.emp_desig FROM emplys;");
if($display == FALSE){
echo "no records found";
}
else{
echo "<div class='tab-pane' id='datatable'>
<header class='panel-heading'> DataTables <i class='fa fa-info-sign text-muted' data-toggle='tooltip' data-placement='bottom' data-title='ajax to load the data.'></i> </header>
<div class='table-responsive'>
<table class='table table-striped b-t text-sm' data-ride='datatables'>
<form action=".$_SERVER['PHP_SELF']." method='post'>
<input type='button' value='Delete' name='delete'>
<thead>
<tr>
<th width='5%'> <input type='checkbox' name='employee_chk' value=""></th>
<th class='th-sortable' data-toggle='class'>Id <span class='th-sort'><i class='fa fa-sort-down text'></i><i class='fa fa-sort-up text-active'></i><i class='fa fa-sort'></i> </span></th>
<th class='th-sortable' data-toggle='class'>Name <span class='th-sort'><i class='fa fa-sort-down text'></i><i class='fa fa-sort-up text-active'></i><i class='fa fa-sort'></i> </span></th>
<th width='30%'>Designation</th>
</tr>
</thead> <tbody> ";
while($row = mysqli_fetch_assoc($display)) {
echo '<tr><td width="5%"> <input name="checkbox[]" type="checkbox" id="checkbox[]" value='.$row["emp_id"].'> </td> <td>'.$row['emp_id'].'</td><td>'.$row['emp_name'].'</td><td>'.$row['emp_desig'].'</td></tr>';
}
echo" </tbody>
</form>
</table>
</div>
</div>";
if (isset($_POST['delete'])){
$id=$_POST['checkbox'];
$N = count($id);
for($i=0; $i < $N; $i++)
{
$result = mysql_query("DELETE FROM emplys where emp_id='$id[$i]'");
}
//header("location: view-employ.php");
}
}
$con->close(); ?>
<input type='checkbox'>
it should have a name, like<input type='checkbox' name='employee_chk'>
.and you can get it like
$_POST['employee_chk']
so your code should be:
UPDATE
<input type='checkbox' name='employee_chk'>
should be inside yourform
element. Also noticed, that your checkbox doesn't contain any values.I think checkbox value needs to be employeeID.