I'm currently struggling as to why my code does not do the following multiple table row deletion php code:
incude("dbconnect.php");
if (isset($_POST["id"])) {
foreach ($_POST["id"] as $id) {
$sql = "DELETE FROM doctor WHERE DOC_LICENSE_NUM='.$id.'";
if ($mydatabase->mysqli_query($sql) === TRUE) {
echo "<div>Record deleted successfully</div>";
} else {
echo "Error deleting record: " . $mydatabase->error;
}
//$sql="DELETE from DOCTOR where DOC_LICENSE_NUM='".$id."'";
//mysqli->query($connect, $sql);
}
//mysqli->close();
}
whereas dbconnect links $mydatabase to the sql. Now, I created a button with the id btn_delete in order to use javascript click function in handling multiple rows to delete in my sql.
<div style="float: right; margin: 0px 10px 10px 0px;"><button type="button" name="btn_delete" class="btn" id="btn_delete" ><span class="fa fa-trash" style="font-size:16px; margin-right:5px;"></span>Delete Multiple</button></div>
What I did is that I used $.ajax in order to gather the checked checkboxes in an array and using the url for my php for deleting rows in the table and if it has entered the php it will proceed to fade the rows out of the table.
$('#btn_delete').click(function(){
if(confirm("Are you sure you want to delete these?")){
var id=[];
$(':checkbox:checked').each(function(i){
id[i]=$(this).val();
//alert(id);
});
if(id.length===0){
alert("Please select at least two checkboxes");
}else{
$.ajax({
url:'muldel.php',
method: 'POST',
data: {id:id},
success: function(){
for (var i=0; i<id.length; i++){
$('tr#'+id[i]+'').css('background-color', '#ccc');
$('tr#'+id[i]+'').fadeOut('slow');
}
//location.reload();
}
});
}
}else{
return false;
}
});
However, when it proceeds to fade the row out of my tables, after I refresh it, it does not delete the rows like how I expected it to. What am I missing out here? (I forgot to mention that I used a unique entity key as my id)