As a part of my training, my team leader asked me to make a simple login in PHP, I have created a controller that receives the username and the password from a JavaScript file, and then triggers an SQL query using parameters, that's where my first problem starts. I execute the statement but then I can't manage to store the query dataset in my $stored
variable. I'm using store_result()
, and neither have any rows being returned ($queryRows variable).
What am I doing wrong?
<?php
include('database.php');
session_start();
//Fetch the user data
$user = $_POST['user'];
$password = $_POST['pass'];
$_SESSION['user'] = $user;
//Checks for empty values
if(empty($user)||empty($password)){
header("Location: ../views/login.php?error=emptyfields");
exit();
}
//DB query to validate the user/password combination
$validationQuery = "SELECT userName, password FROM users WHERE userName =? AND password =?";
//Prevents SQLInjection by using parameters
$statement = $connection->prepare($validationQuery);
$statement->bind_param("ss",$user,$password);
$executed = $statement->execute();
$stored = $statement->store_result($statement);
$queryRows = $statement->num_rows($statement);
if ($stored) {
header("Location: ../views/home.php");
exit();
} else {
header("Location: ../views/login.php?error=userError");
exit();
}
mysqli_stmt_close($statement);
mysqli_close($connection);