Click here to Skip to main content
15,899,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please i need only a small assistance, i do not understand why my select statement does not work, i can login even with invalid login credentials. i hashed my password on the registration page .
$Query = "SELECT *password FROM users WHERE username = ?";
$statement = $conn->prepare($Query);
$statement->bindValue(':username', $username);
$statement->execute();
$user = $statement->fetch(PDO::FETCH_ASSOC);
$RowCount = $statement->rowCount();

} catch (PDOerrorInfo $e){

die('QuerySCD Error '.$e->getMessage());

}
this is the code i used to hash my password
$password =password_hash($_POST['password'], PASSWORD_DEFAULT);


What I have tried:

code altercation, i checked tutorials
Posted
Updated 20-Oct-20 22:48pm

SQL
$Query = "SELECT *password FROM users WHERE username = ?";

Why is there an asterisk in front of password?
 
Share this answer
 
Comments
gavin_daCEO 21-Oct-20 4:34am    
To tell the code and made some adjustments so the asterisk was there and i did not alter the query statement.
gavin_daCEO 21-Oct-20 4:37am    
i deleted the asterisk but the error still persists
Richard MacCutchan 21-Oct-20 4:55am    
What error? Please provide proper complete details of your problem.
gavin_daCEO 21-Oct-20 5:20am    
i was able to login with invalid login credentials but then after deleting the asterik and the cache on my computer it works, the only problem that just started is that even when i provide valid login credentials it does not work. Let me check my codes if i am missing something and also if you have any suggestion please suggest them
Richard MacCutchan 21-Oct-20 5:31am    
When we ask for complete details of the problem, saying "it does not work" tells us nothing about what or where the problem may be.
First, how come you're using username = ? but then trying to bindValue(':username', $username);? I would expect you to use bindValue(1, $username); because there's no named parameter in your query.

Second, if the issue is that it's allowing you to login regardless of which password you enter, you've omitted the part of the code which checks whether the password is correct. All the code that you've provided does is get any matching users, and get the row count, and that's all it does? I would expect to see some code which checks if the row count is one, and compares the submitted and stored passwords.
 
Share this answer
 
Comments
gavin_daCEO 21-Oct-20 5:23am    
this is the complete code for the login page


if($_SERVER['REQUEST_METHOD'] == 'POST'){

$username = trim($_POST['username']);

try{

$Query = "SELECT * password FROM users WHERE username = :username";
$statement = $conn->prepare($Query);
$statement->bindValue(':username', $username);
$statement->execute();
$user = $statement->fetch(PDO::FETCH_ASSOC);
$RowCount = $statement->rowCount();

} catch (PDOerrorInfo $e){

die('QuerySCD Error '.$e->getMessage());

}

if( $RowCount == 0 ){

$_SESSION['message'] = "error!";

$message = "Invalid login credentials";

} else{ // User exists

if( password_verify($_POST['password'], $user['password'])){

$_SESSION['username'] = $user['username'];
$_SESSION['active'] = $user['active'];

$_SESSION['logged_in'] = true;

header("location: studentportal.php?onions=no&pickles=yes");

} else {

$_SESSION['message'] = "error!";
//header("location: error-login.php");
$message = "Invalid login credentials";

}
}
}

$conn = NULL;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900