How to update php correctly

Asked

Viewed 472 times

1

Hello I am making an administrative panel where the administrator can edit the Name, User and email but it does not perform the UPDATE in the database but in the message it says that the update follows the Update code.

<?php
    include_once("conexao.php");
    $servidor = "fdb17.atspace.me";
    $usuario = "2443930_braban";
    $senha = "minhasenha";
    $dbname = "2443930_braban";

    //Criar a conexao
    $conn = mysqli_connect($servidor, $usuario, $senha, $dbname);

    //selecionando o banco de dados
    $db = mysqli_select_db($conn, "2443930_braban");

    //criando a query de consulta à tabela criada
    $sql = mysqli_query($conn, "SELECT * FROM usuarios") or die(
        mysqli_error($conn) //caso haja um erro na consulta
    );

    $id                 = $_POST["id"];
    $nome               = $_POST["nome"];
    $email              = $_POST["email"];
    $usuario            = $_POST["usuario"];

    $result_usuario = "UPDATE usuarios set nome ='$nome', usuario = '$usuario', email = '$email', WHERE id='$id'";

    $resultado_usuario = mysqli_query($conn, $result_usuario);
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
</head>

<body>
    <?php
        if (mysqli_affected_rows($conn) != 0 ){ 
            echo "
                <META HTTP-EQUIV=REFRESH CONTENT = '0;URL=http://braban.atspace.tv/dados/admin/ccbo/adm2/administrativo.php?link=2'>
                <script type=\"text/javascript\">
                    alert(\"Usuário editado com Sucesso.\");
                </script>
            ";         
        }
        else{   
            echo "
                <META HTTP-EQUIV=REFRESH CONTENT = '0;URL=http://braban.atspace.tv/dados/admin/ccbo/adm2/administrativo.php?link=2'>
                <script type=\"text/javascript\">
                    alert(\"Usuário não foi editado com Sucesso.\");
                </script>
            ";
        }
    ?>
</body>
</html>
  • Tip : Under your mysqli_query add printf("Error message: %s n", mysqli_error($Conn)); Which message gives ?

  • not error message says User edited successfully but does not edit anything

  • Added the printf with mysqli_error($Conn) after running your querie ?

  • $result_usuario = "UPDATE usuarios set name ='$name', usuario = '$user', email = '$email', WHERE id='$id'"; has no comma after ", email = '$email', WHERE" . Take off that comma!

  • added raphael showed no error and pulled out the vergula now shows User has not been edited successfully.

  • So when it is like this and leaves lost agent, usually before the query we have it printed like this : echo "UPDATE usuarios set name ='$name', user = '$user', email = '$email', WHERE id='$id'"; we take the result and run directly inside the bank, must have some variable sent by empty POST like $id; mysql_error should report error in the querie. Print the querie on the page and check the result.

  • if I put a SESSION will work would?

Show 3 more comments
No answers

Browser other questions tagged

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