Reputation: 2063
I have created a button on my application that is supposed to remove a record in my database. At the moment I get a message saying the record has been successfully removed but it isn't actually deleting it.
Here is my button HTML
<button id="deleteButton" class="btn btn btn-danger btn-lg active" role="button" value="<?php echo $rows['recipe_id']; ?>">Delete Recipe</button>
I first send the ID of the record via ajax with this code
$(document).ready(function() {
$('#deleteButton').click(function(){
var clickBtnValue = $(this).val();
var recipeID = JSON.stringify(clickBtnValue);
var data = {
"action" : recipeID
}
var ajaxurl = '/recipe-project/ajax.php';
$.ajax({
url: ajaxurl,
type: "POST",
data: data,
success:function(data) {
alert(data);
}
});
});
});
And here is my PHP
include_once('classes/class-database-functions.php');
include_once ('classes/User.php');
if (isset($_POST['action'])) {
$recipe_id = $_POST['action'];
$test = new User();
if (!empty($test)) {
$test->delete_recipe($recipe_id);
}
}
and here is the function in the class that actual removes the record
public function delete_recipe($recipeID) {
$query = "DELETE FROM recipes WHERE recipe_id= '$recipeID'";
if (mysqli_query($this->connection, $query)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($this->connection);
}
}
I am not getting errors in my logs and i've checked the ajax is sending the correct ID to the function, can anyone see why this problem is occurring?
Upvotes: 1
Views: 39
Reputation: 157
As it's a POST, this part of your code is good:
if (isset($_POST['action'])) {
$recipe_id = $_POST['action'];
$test = new User();
if (!empty($test)) {
$test->delete_recipe($recipe_id);
}
}
But for your debug, you can have before this code:
var_dump($_POST);
You can also installed firebug on Firefox in order to debug which datas you are sending.
Upvotes: 1
Reputation: 1600
I suggest you change your code to this
if (isset($_GET['action'])) {
$recipe_id = $_GET['action'];
$test = new User();
if (!empty($test)) {
$test->delete_recipe($recipe_id);
}
}
then test it by accessing the URL directly in your browser and including the $_GET variable, e.g. ?action=the recipe's id
. Then you can use var_dump
to debug if the ID is correct, if your $test
object is created correctly and if the query is executed properly.
Upvotes: 0