The record is not found in the database when the user logs in

Asked

Viewed 27 times

1

I am trying to verify the existence of a user in the database when it performs the login. The user and password are already created in the database, but even then the check only enters the else.

logging in.php

<?php
include_once("conexao.php");
session_start();

$usuario = $_POST['usuario'];
$senha = $_POST['senha'];


$result = mysqli_query("SELECT * FROM `usuarios` WHERE `usuario` = '$usuario' AND `senha`= '$senha'");

if(mysqli_num_rows($result) > 0 )
{
$_SESSION['usuario'] = $usuario;
$_SESSION['senha'] = $senha;
header('location:teste1.php');
}
else{
    unset ($_SESSION['usuario']);
    unset ($_SESSION['senha']);
    header('location:teste2.php');

    }

?>

login.php

<?php
  include_once("conexao.php");
  session_start();
?>

<form class="login-form" name="loginform" method="post" action="logando.php">
    <input type="text" placeholder="Usuário" name="usuario" required=""/>
    <input type="password" placeholder="Senha" name="senha" required=""/>
    <button type="submit">ENTRAR</button>
    <p class="message"> Desenvolvido por: Marcos A. Massini</p>
</form>

<p class="text-center text-danger">
    <?php
        if(isset($_SESSION['loginErro'])) {
            echo $_SESSION['loginErro'];
            unset($_SESSION['loginErro']);
        }
    ?>
</p>

php connection.

<?php
  $servidor = "localhost";
  $usuario = "root";
  $senha = "";
  $dbname = "cadastro";

  $conn = Mysqli_connect($servidor, $usuario, $senha, $dbname) or print (mysql_error());

  if(!Mysqli_connect($servidor, $usuario, $senha)) {
    echo "Error ao conectar";
  }
?>
  • You are encrypting passwords when registering in the database ?

  • No no, the password is normal, login: free password: free, nothing encrypted.

  • Your database connection is working ?

  • Yes, the registration is working normally with this connection.

  • Before the condition if .. else makes a print_r($result); or var_dump($result); to know if something is returning.

  • Oops, I got it. I put it this way:

  • $result = "SELECT * FROM usersWHEREuser= '$usuario' ANDpassword= '$senha'";&#xA;$resultado = mysqli_query($conn, $result); Não sei como faz pra ficar em formato de codigo...

Show 2 more comments

1 answer

1

I believe the problem is occurring because you are not passing the appropriate parameters.

According to the documentation php, the mysqli_query() asks for 2 arguments:

  1. mysqli link - The link to the connection.

  2. string query - The query you want to run.

With this, try to execute your query by also passing the link:

mysqli_query($conn, "SELECT * FROM `usuarios` WHERE `usuario` = '$usuario' AND `senha`= '$senha'");

Browser other questions tagged

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