Validation php only returns one value

Asked

Viewed 51 times

0

My login validation is always returning the same value in my $Row variable.

Follows the code:

php connection.

<?php
define('HOST', '127.0.0.1');
define('USUARIO', 'root');
define('SENHA', '');
define('DB', 'login');

$conexao = mysqli_connect(HOST, USUARIO, SENHA, DB) or die('Não foi possível conectar');

login.php

<?php
session_start();
include('conexao.php');

if(empty($_POST['usuario']) || empty($_POST['senha'])) {
    header('Location: index.html');
    exit();
}

$usuario = mysqli_real_escape_string($conexao, $_POST['usuario']);
$senha = mysqli_real_escape_string($conexao, $_POST['senha']);

$query = $query = "select usuario_id, usuario from usuario where usuario = '$usuario' and senha = md5('$senha')"; 

$result = mysqli_query($conexao, $query);

$row = mysqli_num_rows($result);

if($row == 1) {
    $_SESSION['usuario'] = $usuario;
    header('Location: cadastro.html');
    exit();
} else {
    header('Location: index.html');
    exit();
}

In my if($Row..) is always returned the Else, even with the user registered in the database, that is, independent of the user typed, wrong or not, the function redirects to index.html

Table structure

inserir a descrição da imagem aqui

Note: The connection with the comic was successfully performed.

1 answer

0


Assuming that your consent index.html is the file that has the login form, you need to inform the element <form> where it should send the data when form submitted.
You can do this using the attribute action of that element.

See the html code with the action attribute pointing to your page login.php

<form method="POST" action="login.php">
    <input type="text" name="usuario" />
    <input type="password" name="senha" />
    <input type="submit" name="enviar" />
</form>

Note The tag <form> without the attribute action will always point to the page where the form is inserted if this attribute is omitted, so you were always redirected to it and the file login.php nor was it even triggered.

  • Hi friend, my form is as you wrote, and still with error.

  • You can post the contents of the index.html file?

  • I discovered the error. When the field did the validation, it was searching for the password in the database user column. In my comic, I changed the name of the user and password columns, one by the other. Then it worked right.

Browser other questions tagged

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