Hello. I'm creating a login system for study purposes.
I have identified in the file that is not populating the array with the information of the database ... and yes, I already made sure that the entered email and password already exists in the database.
Following login.php file:
<?php
require_once 'init.php';
// resgata dados digitados no formulario
$email = isset($_POST['email']) ? $_POST['email']: '';
$senha = isset($_POST['senha']) ? $_POST['senha']: '';
// Cria o hash da senha
$seg_senha = password_hash($senha, PASSWORD_DEFAULT);
// Verifica se os campos do form nao estao vazios
if(empty($email) || empty($senha)){
echo 'Informe Email e Senha';
exit;
}
// Comando no banco de dados
$pdo = db_connect(); // Abre conexão com o banco
$sql = "SELECT id, nome FROM usuarios WHERE email = :email AND senha = :senha";
// Cria query
$stmt = $pdo->prepare($sql); // Prepare da query
$stmt->bindParam(':email', $email);
// Atribui valor do campo email no valor email da query
$stmt->bindParam(':senha', $seg_senha);
// Atribui valor do campo senha no valor senha da query
$stmt->execute(); // Execute na query
$arr = $stmt->fetchAll(PDO::FETCH_ASSOC); // Cria array associativo
if(count($arr) <= 0){ // Verifica se existe elemento no array
echo "<script language='javascript' type='text/javascript'>alert('Login e/ou senha incorretos');window.location.href='../login.php';</script>";
exit;
}
// Pega o primeiro usuario
$user = $arr[0];
// Inicia a sessão
session_start();
$_SESSION['logged_in'] = true;
$_SESSION['user_id'] = $user['id'];
$_SESSION['user_name'] = $user['nome'];
header('Location: ../index1.php');
If I comment from the line if(count($arr) <= 0){
down, and I put a var_dump($arr);
below $arr = $stmt->fetchAll(PDO::FETCH_ASSOC);
it returns an empty array
C:\wamp64\www\ProjetoALPHA\core\login.php:33:
array (size=0)
empty
Could anyone help me?
Thank you!