I have a table of Data which is pulled via SQL into DataTables. I want to use AJAX to run an SQL query which deleted the row based on $id = $row["id"];
.
Index.php:
$link = mysqli_connect("localhost", "bradlyspicer_root", "", "bradlyspicer_ResellerDB");
$id = $_POST['id'];
$deleterow = "DELETE FROM Offences WHERE id = ?";
if($stmt = mysqli_prepare($link, $deleterow)){ // $link being your connection
mysqli_stmt_bind_param($stmt, "s", $id);
mysqli_stmt_execute($stmt);
echo 'success';
echo $id;
} else {
echo 'fail!';
printf("Error: %s.\n", mysqli_stmt_error($stmt));
}
Functions.php:
$id = $_POST['id'];
$deleterow = "DELETE FROM Offences WHERE id = ?";
if($stmt = mysqli_prepare($link, $deleterow)){ // $link being your connection
mysqli_stmt_bind_param($stmt, "s", $id);
mysqli_stmt_execute($stmt);
echo 'success';
} else {
echo 'fail!';
printf("Error: %s.\n", mysqli_stmt_error($stmt));
}
Custom.js:
$( ".delbtn" ).click(function(){
var itemID = $(this).attr("itemID");
console.log(itemID)
$.ajax({
url:"functions.php", //the page containing php script
data: { id: itemID}, // itemID passed as id
type: "POST", //request type
success:function(result){
alert(result);
},
error: function() {
alert('Error occured');
}
});
});
I can't find where I pass the $id in the button from Index.php to Functions.php, any explanation would be appreciated.
Update: Since updating the script and trying to Debug, I'm not getting much of a response from the error which outputs:
fail!Error: .