Request jQuery Post + PHP + MySQL does not write data

1

I'm doing a registration via jQuery Ajax, so I created the following function:

function salvarCadastro(titulo, form, caminho) {
    var dadosFormulario = $("#" + form).serialize();

    var myData = $("#" + form).serialize();
    jQuery.ajax({
        type: "POST",
        url: caminho,
        dataType: "text",
        data: myData,
        cache: false,
        success:function(response){
            data = data.trim();
            if (data == 'S') {
                alert('Dados registrados com sucesso.');
            } else {
                alert('Não foi possivel registrar os dados!');
            }
        },
        error:function (xhr, ajaxOptions, thrownError){
            alert('Houve um prolema de requisição ao ' + titulo');
        }
    });

    return false;
}

In my php file it looks like this:

<?php
    session_start();
    $mysql = new mysqli('localhost', 'root', '', 'teste');

    $id        = $_POST['id'];
    $ordem     = $_POST['ordem'];
    $descricao = utf8_decode(trim($_POST['descricao']));

    $sql = "UPDATE dados SET ordem = '$ordem', descricao = '$descricao' WHERE id = $id";

    $mysql->query($sql);
    if (empty($mysql->error)) {
        echo "S";
    } else {
        echo "N";
    }
    $mysql->close();
?>

If I do a post directly from the form to the PHP file it returns me "S" the same occurs via Ajax, but via Ajax it does not record the information in the database.

    
asked by anonymous 19.02.2015 / 19:07

1 answer

1

It has an easier way that I use. Try the code in PHP, instead of "S" and "N" put echo "<script> alert ('mensagem'); <\script>"; or it's something in the query string.

I do this and it always worked.

 $sql = "UPDATE dados SET ordem = '".$ordem."', descricao = '".$descricao."' WHERE id = ".$id;
    
27.02.2015 / 03:53