How to present the error coming from MySQL?

3

I made the code below, and it works perfectly, but if MySQL returns an error, how will I know?

I have no idea, can anyone help me?

The message should return in alert , however I was able to create alert of Sucesso , however how do I 'alert' with error return when 'false' ???

Follow the code:

var nome_arquivo_api = "clientes.php";    
var end_arquivo_api = "api/include/";
var complemento_api = "?action=";  
var lista_excluir_api = "lista_excluir";

var url_excluir_api = end_arquivo_api+nome_arquivo_api+complemento_api+lista_excluir_api;   

$scope.apagarRegistro = function (id) {
    for (i in $scope.listagem) {
        if ($scope.listagem[i].id == id) { 
            $http.post(url_excluir_api,
                    {
                        'id': id
                    }
            )
            .success(function (data, status, headers, config) {
                $http.get(url_listar_api).success(function (data) {
                    $scope.listagem = data;
                    $.gritter.add({
                        title: '<div style="color:#43A608;">ATENÇÃO</div>',
                        text: 'Registro Excluído com Sucesso!',
                        image: 'assets/images/icon_sucesso.png',
                        sticky: false,
                        time: ''
                    });
                });
            });
        }
    }
}

PHP file:

function lista_excluir() {

$data = json_decode(file_get_contents("php://input"));
$item_id = $data->id;

if (PDO_excluirRegistro("administradores", $item_id, "id")) {
    return true;
} else {
    return false;
}
}

PHP function:

function PDO_excluirRegistro($tabela, $id, $campo_chave) {
$pdo = conectarBanco();
try {
    $deletar = $pdo->prepare("DELETE FROM $tabela WHERE $campo_chave = ?");
    $deletar->bindValue(1, $id);
    $deletar->execute();

    if ($deletar->rowCount() == 1) :
        return true;
    else :
        return false;
    endif;
} catch (PDOException $error) {
    echo "<h4>";
    echo "Mensagem de Erro: " . $error->getMessage();
    echo "</h4>";
}}
    
asked by anonymous 30.07.2015 / 03:21

1 answer

3

Your PHP code needs to return an HTTP code that is not 200 . Simply returning true or false is not enough - in both cases, $http.post() returns successfully , but simply success has the value of false .

In PHP, you would have to change the Headers of the message back. A very simple code would be to use the header() function:

function lista_excluir() {

$data = json_decode(file_get_contents("php://input"));
$item_id = $data->id;

if (PDO_excluirRegistro("administradores", $item_id, "id")) {
    header("HTTP/1.1 200 OK");
    return true; // Aqui, você devolve o valor 'true', porém poderia conter HTML ou JSON também
} else {
    header("HTTP/1.1 500 Internal Server Error");
    return false; // Mesmo aqui
}
}

More information

Web technologies (such as javascript, and therefore angularJS) use the HTTP / 1.1 standard. This pattern defines multiple return codes. They call HTTP Status Codes

30.07.2015 / 05:23