Help when deleting a record with ajax and php

0

I have a CRUD but instead of users they are all managed with Ajax .

The issue is that the update create works but in delete that is exactly the same as the others do not.

Step to hang in code:

$('#deleteUser').click(function() {
if($('.checkBox:checked').val() == undefined){
    if(!$('#message').hasClass("alert alert-danger")) {
        $('#message').addClass("alert alert-danger").append("<a class='close'>×</a> <p>Tienes que seleccionar alguna pregunta</p>");
    }
    $('.close').click(function(){
        clearMessage();
    });
}else{
    $('#message').removeClass("alert alert-danger").empty();
    if(confirm("Estas seguro de que quieres eliminar la pregunta?")){
        $.ajax({
            type: 'POST',
            data:{
                funcion: 'deleteQuestion',
                Id: $('.checkBox:checked').val(),
            },
            url: '../php/questionsAdmin.php',
            success: function(data){
                if(data == 1){
                    loadQuestions();
                    addDoneButton("Pregunta eliminada correctamente");
                    $('.close').click(function(){
                        clearMessage();
                    });

                }else{
                    alert("algo no ha ido bien");
                }

            }

        })

    }
  }
});

The delete question needs the question to have checkbox enabled, the first if checks that this question is selected and then checks that there is no error message in div . Then make the call to ajax by passing the id of checkbox (which is the id of the question previously loaded with ajax), if the success of the call is true message that everything went well but a alert . There goes the php code:

function deleteQuestion($mysqli,$id){
$select = "DELETE FROM question WHERE Id = :id";
$row = $mysqli->prepare($select);
$row->execute(array(":id" => $id));

if($row->rowCount() == 1){
    echo 1;
}else{
    echo 0;
  }
}

$mysqli is the variable in the connect file (which is the one that connects to the database use PDO ), I checked if it was wrong and the connection is good, I have tried the query that does this function manually in mysql and if that works, I've also looked at the variable $id is well collected and if it collects well, other clarifications is that the fields and table is also well set. / p>     

asked by Adria Tomas Altes 19.04.2017 в 11:52
source

0 answers