I have an issue with the Prepared Select Statement and storing and fetching the results. The following code allows users to log in. The code first checks if there is a matching record in the database and then it will redirect the user to Welcome page. I am pretty sure that the problem is with the statement: $stmt->num_rows; and then with fetching the results from $stmt. Any help would be greatly appreciated.
i have the following code:
if ($user == "" || $pass == "")
{
$error = "<span class='error'>You must enter the username and password</span><br /><br />";
}
else
{
$query = "SELECT UserName,Password,Role FROM Users
WHERE UserName=? AND Password=?";
$stmt = $db->prepare($query);
$stmt->bind_param("ss", $user, $pass);
$stmt->execute();
$stmt->store_result();
$stmt->num_rows;
if ($stmt == 0)
{
$error = "<span class='error'>Incorrect Username/Password
</span><br /><br />";
}
else
{ for ($i=0; $i <$stmt; $i++) {
$row = $stmt->fetch_assoc();
$role = $row['Role'];
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
$_SESSION['role'] = $role;
header( "Location: welcome.php" ); //will redirect to pool list after succesful login
die ("You are now logged in. ");
}
}
}
}
It seems that i figured it out. The code is as follows:
$query = "SELECT UserName,Password,Role FROM Users
WHERE UserName=? AND Password=?";
$stmt = $db->prepare($query);
$stmt->bind_param("ss", $user, $pass);
$stmt->execute();
$stmt->store_result();
$numrows = $stmt->num_rows;
$stmt->bind_result($user, $pass, $role);
if ($numrows == 0)
{
$error = "<span class='error'>Incorrect Username/Password
</span><br /><br />";
}
else
{ for ($i=0; $i <$numrows; $i++) {
$row = $stmt->fetch();
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
$_SESSION['role'] = $role;
header( "Location: welcome.php" ); //will redirect to pool list after succesful login
die ("You are now logged in. Please <a href='members.php?view=$user'>" .
"click here</a> to continue.<br /><br />");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.