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);
    }
    ?>
7
  • put $id in quote in query '$id' Commented Sep 16, 2015 at 12:51
  • Your script is at risk for SQL Injection attacks. Commented Sep 16, 2015 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 Commented Sep 16, 2015 at 12:54
  • 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. Commented Sep 16, 2015 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. Commented Sep 16, 2015 at 13:20

1 Answer 1

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}'"
Sign up to request clarification or add additional context in comments.

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.