Script shows success message, but does not update the database

Asked

Viewed 30 times

-1

Why the bank is showing the message of success, but is not updating the bank?

<?php
$conectar = mysqli_connect ("localhost", "root","","usuario_noticias"); 
    $nome = $_POST["nome"];
    $login = $_POST["login"];
    $senha = $_POST["senha"];
    $cod = $_POST["codigo"];


    $sql_pesquisa = "SELECT 
                            login_user
                                FROM usuario WHERE login_user='$login' and cod_user = '$cod'";

    $resultado_consulta = mysqli_query ($conectar, $sql_consulta);

    $linhas = mysqli_num_rows ($sql_resultado);

        if($linhas==1){

            echo "<script> alert ('Login já existe. Tente de novo.') </script>";

            echo "<script>location.href = ('altera_exclui_user.php') </script>";
            exit();

        }
        else {
            $sql_altera = "UPDATE usuario  
                                SET
                                    nome_user='$nome',
                                    login_user='$login',
                                    senha_user='$senha'
                                WHERE
                                 cod_user = 'cod'";

            $slq_resultado_alteracao = mysqli_query ($conectar, $sql_altera);

                    if($slq_resultado_alteracao==true){

                        echo"<script> alert ('$nome alterado com sucesso') </script>";
                        echo "<script>location.href = ('altera_exclui_user.php') </script>";
                            exit();
                        }

                    else{
                        echo"<script> alert ('Problemas com o servidor. Tente novamente!') </script>";
                        echo "<script>location.href = ('altera_exclui_user.php') </script>";

                        }                   


        }

?>
  • 1

    The Where seems to be wrong, would not be so: WHERE&#xA; cod_user = '$cod'"?

  • As @Ricardopunctual commented, it presents the message of success because it was successfully completed, the UPDATE is being performed in the record where cod_user is equal to 'Cod'

1 answer

4

Not getting the correct value of the code on WHERE, needs to change to take $cod, thus:

$sql_altera = "UPDATE usuario  
                  SET
                      nome_user='$nome',
                      login_user='$login',
                      senha_user='$senha'
                 WHERE
                      cod_user = '$cod'"; 
  • I did it, but it continued with the same problem.

  • Type the value of the variable $sql_altera on the page and see everything is correct, if running directly the command in the bank works, if it is getting the correct value in the variable $cod. If the field cod_user is numeric does not need single quotes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.