php login giving user nonexistent

Asked

Viewed 64 times

-3

I created a login system with php and mysql but I can’t log in even though I’m sure the data has been registered successfully. I don’t know what I did wrong. Look at the code:

    <?php
         $login2 = $_POST['loginlogar'];
         $senha2 = $_POST['senhalogar'];
         
         $link = mysqli_connect("localhost", "root", "", "bancolanche")
                 or die("<h1>Não foi possível conectar!!</h1>".mysqli_error());
         $banco = mysqli_select_db($link, "bancolanche")
                 or die("<h1>Não foi possível abrir banco de dados!!</h1>". mysqli_error());
         
        $sql = "SELECT login, senha FROM users";
        $resulta = mysqli_query($link, $sql);
        
         while ($book = $resulta->fetch_object()) {
           $logindb = $book->login;
           $senhadb = $book->senha;
         }

          if($login2 == "" || $senha2 == ""){
              $_SESSION['msg'] = "<h1 style='color:red'>Erro ao logar! Há campos de preenchimento obrigatório em branco.</h1>";
              header("Location: logar.php");
          } else {
              if($logindb==$login2 && $senhadb==$senha2){
              $_SESSION['msg'] = "<h1 style='color:green'>Logoff realizado com sucesso!</h1>";
              header("Location: index.html");
          } else {
              $_SESSION['msg'] = "<h1 style='color:red'>Erro ao logar! Usuário inexistente ou senha incorreta.</h1>";
              header("Location: logar.php");
          }
          }      
          mysqli_close($link);
     ?>
  • 1

    Hello! Your code has a closing key left ( } ) IF with two ELSE does not work unless a condition is passed for the first ex: if ($x == $y) {} elseif($x == $z) {} else {}

2 answers

-2

Try to check if this way you can. Note: You can not make the comparison with fields in equal white made, example: if($login2 == "" || $senha2 == ""), Remember that this way the field you are comparing is not NULL. I made a small modification, I did not test, I hope it works. Another situation! Treat the $_SESSION out of the code to do the checking, so I recommend you to name specific to each session.

<?php
         $login2 = trim($_POST['loginlogar']);
         $senha2 = trim($_POST['senhalogar']);
         
         $link = mysqli_connect("localhost", "root", "", "bancolanche");
         if($link->connect_error){
              die("Impossível se conectar com o banco de dados");
          }  
         $banco = mysqli_select_db($link, "bancolanche") or die("<h1>Não foi possível abrir banco de dados!!</h1>". mysqli_error());
         
        $sql = "SELECT login, senha FROM users";
        $resulta = mysqli_query($link, $sql);
        
         while ($book = $resulta->fetch_object()) {
           $logindb = $book->login;
           $senhadb = $book->senha;
         }

         if(empty($login2) || empty($senha2)){
            $_SESSION['msg'] = true;
            header("Location: logar.php");
         } else if ($logindb == $login2 && $senhadb == $senha2){
            $_SESSION['msg'] = true;
            header("Location: index.html");
         }else {
            $_SESSION['login_incorretos'] = true;
            header("Location: logar.php");
         }

          mysqli_close($link);
     ?>

-2

You can check whether the user exists directly in the database using WHERE:

"SELECT login, senha 
    FROM users 
      WHERE login = '{$login2}' AND senha = '{$senha2}' LIMIT 1"

This way Cvoce can select your WHILE in PHP... And you should also just connect and check in the database if the user filled in the required fields... Your code would be something like:

$login2 = $_POST['loginlogar'];
$senha2 = $_POST['senhalogar'];

if(empty($login2) || empty($senha2)){
    $_SESSION['msg'] = "<h1 style='color:red'>Erro ao logar! Há campos de preenchimento obrigatório em branco.</h1>";
    header("Location: logar.php");
}else{
    //Connecta
    $link = mysqli_connect("localhost", "root", "", "bancolanche") or die("<h1>Não foi possível conectar!!</h1>".mysqli_error());
    $banco = mysqli_select_db($link, "bancolanche") or die("<h1>Não foi possível abrir banco de dados!!</h1>". mysqli_error());
    //faz o select
    $sql = "SELECT login, senha FROM users WHERE login = '{$login2}' AND senha = '{$senha2}' LIMIT 1";
    $resulta = mysqli_query($link, $sql);
    mysqli_close($link);
    //verifica se existe
    if (mysqli_num_rows($resulta) != 0){
        $_SESSION['msg'] = "<h1 style='color:green'>Login realizado com sucesso!</h1>";
        header("Location: index.html");
    }else{
        $_SESSION['msg'] = "<h1 style='color:red'>Erro ao logar! Usuário inexistente ou senha incorreta.</h1>";
        header("Location: logar.php");
    }
}
  • I used your code, but the result was the same. I don’t know what else to do.

  • You must not use my code, but understand the logic of what has been done... You need to debug your code and find what is wrong... it is very vague only "I can’t login", You need to understand exactly where the information or logic that is problematic... Start debugging the database result... When you select to the Databank what is the result? If you do Select directly in the bank what is the result? and so on until we find what is wrong... unfortunately we do not have access to your bank/ code to reproduce exactly the same...

Browser other questions tagged

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