1

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.