1

I am trying to update my sql database with php, but is not working.

This is the function that calls the php:

$("#btAtualizarEndereco").click(function(){

                var telAtualizado = $("#telefonePedido").val(); 
                alert(telAtualizado);
                var idAtualizado = $("#idContato").val();
                alert(idAtualizado);
                var enderecoAtualizado = $("#enderecoPedido").val();
                alert(enderecoAtualizado);
                var numeroAtualizado = $("#numeroPedido").val();
                alert(numeroAtualizado);
                var bairroAtualizado = $("#bairroPedido").val();
                alert(bairroAtualizado);
                var complementoAtualizado = $("#complementoPedido").val();
                alert(complementoAtualizado);
                var pontoRefAtualizado = $("#pontoRefPedido").val();
                alert(pontoRefAtualizado);

                $.ajax({
                    url: "atualizarEndereco.php",
                    type: "POST",
                    data: {
                        tel : telAtualizado,
                        id : idAtualizado,
                        endereco : enderecoAtualizado,
                        numero : numeroAtualizado,
                        bairro : bairroAtualizado,
                        complemento : complementoAtualizado,
                        pontoRef : pontoRefAtualizado
                    },
                    cache: false,
                    processData:true,
                    success: function(data)
                    {
                        alert("passou no php");
                    }
                });
            });

This function works, and i have the alert on sucess.

This is my php called:

<?php

// Conexao com o BD
require_once "admin/conexao.php";

$id = $_POST['id'];
$tel = $_POST['tel'];
$endereco = $_POST['endereco'];
$numero = $_POST['numero'];
$bairro = $_POST['bairro'];
$complemento = $_POST['complemento'];
$pontoRef = $_POST['pontoRef'];

$sqlNovoContato = mysqli_query("UPDATE contato SET telefone = '$tel' ,    endereco = '$endereco',
 numero = '$numero', bairro = '$bairro', complemento = '$complemento', pontoReferencia = '$pontoRef' WHERE idContato = $id");

?>    

The update doesn't work.

This is conexao.php:

   <?php

    $conexao = mysqli_connect('localhost', 'root', '', 'db123Pastel');
    // Checando a conexao
    if($conexao->connect_errno > 0){
    die('Falha na conexao com o banco de dados ['. $conexao->connect_errno    .']');
    }

    if(!$conexao->set_charset("utf8")) {
    printf("Erro ao carregar character set utf8: %s\n", $conexao->error);
    }
    ?>
  • put `$id` in quote in query `'$id'` – Shehary Sep 16 '15 at 12:51
  • Your script is at risk for SQL Injection attacks. – Jay Blanchard Sep 16 '15 at 12:53
  • 1
    You don't need to complecate matters by bringing ajax into the picture. Just test your PHP script separately. And when you say not working explain what not working means – e4c5 Sep 16 '15 at 12:54
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Sep 16 '15 at 13:08
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. **Questions without a clear problem statement are not useful to other readers**. "Doesn't work" tells us very little about the problem. – Quentin Sep 16 '15 at 13:08
  • 1
    Hello, when i said "doesn't work" my problem was that the database wasn't updating. But I fix it and is updating now. About the SQL injection i will fix this issue. Thank for the alert. – Ricardo Afonso Sep 16 '15 at 13:20
  • @RicardoAfonso switch to PDO will fix the issue with injects and it does not require a lot of changes to your code.. – DTH Sep 16 '15 at 13:33

1 Answers1

1

You can echo the query and check for any syntax errors. Run that query my MySQL console and validate the query. may be you can use the following syntax for query.

    "UPDATE contato SET telefone = '{$tel}' ,    
            endereco = '{$endereco}',
            numero = '{$numero}', 
            bairro = '{$bairro}', 
            complemento = '{$complemento}', 
            pontoReferencia = '{$pontoRef}' 
      WHERE idContato = '{$id}'"
Trinimon
  • 13,839
  • 9
  • 44
  • 60
Saurabh
  • 776
  • 1
  • 5
  • 15