I'm creating a valid credential function for a log in page.
The function works if I use it outside the function using the following code:
if(isset($_POST['email'])){
$email = $_POST['email'];
$password = $_POST['password'];
$q = "SELECT * FROM user WHERE email = :email";
$query = $db->prepare($q);
$query->execute(array(":email" => $email));
$results = $query->fetchAll();
if($results !=FALSE && $query->rowCount() > 0) {
if($results[0]['password'] == $password){
$_SESSION['email'] = $email;
}
But if I call the function from the external function, nothing happens as if there was something wrong with the log in credentials.
the function:
function valid_credientials($email,$password){
global $db;
$email = $_POST['email'];
$password = $_POST['password'];
$q = "SELECT * FROM user WHERE email = :email";
$query = $db->prepare($q);
$query->execute(array(":email" => $email));
$results = $query->fetchAll();
if($results !=FALSE && $query->rowCount() > 0) {
if($results[0]['password'] == $password){
$_SESSION['email'] = $email;
}}
}
and the isset
post:
if (isset($_POST['email'], $_POST['password'])){
if (valid_credientials($_POST['email'], $_POST['password']) == false ){
$errors[] = 'No matching email found.';
}
if (empty($errors)){
$_SESSION['email'] = htmlentities($_POST['email']);
header("Location: profile.php");
die();
}
}
I was thinking about trouble with the connection, but if I use this function it returns the emails.
function valid_credientials($email,$password){
$test = $db->query("SELECT `email` FROM `user`");
while($row = $test->fetch(PDO::FETCH_ASSOC)){
echo $row['email'], '<br>';
}
}