Hey Everyone-
I am relatively new to PHP and am trying to get a login page to validate a user name and password with a MySQL database.
i have a function.php file that checks the database and either validates the user information or returns an error.
I am stuck on an error that is being returned to me because line 32 seems to have no issues with the syntax and the variables look like they are all defined and being called correctly.
any help would be greatly appreciated. Thanks so much.
line 32 is: $r = @mysqli_query($connection, $q); //run the query
here is the code:
<?php
function check_login ($connection, $user_name='', $user_pass='') {
$errors = array(); //Initialize the error array
//validate the e-mail address
if (empty($user_name)) {
$errors[] = 'Oops, you forgot to enter a user name.';
} else {
$n = mysqli_real_escape_string($connection, trim($user_name));
}
//validate the password
if (empty($user_pass)) {
$errors[] = 'Opps, you forgot to enter a password.';
} else {
$p = mysqli_real_escape_string($connection, trim($pass));
}
if (empty($errors)) {
//if everything checks out ok
//retrieve the user_id and first_name for that username and password combination
$q = "SELECT users_id, user_name
FROM users
WHERE user_name = '$n'
AND user_pass = '$p';"
$r = @mysqli_query($connection, $q); //run the query
//check the result:
if (mysqli_num_rows($r) == 1) {
//fetch the record
$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);
//return true and the record
return array(true, $row);
} else {
//not a match!!
$errors[] = 'Sorry, the user name and/or password you provided do not match those on file.';
}
} // end of empty errors IF
//return false and the errors
return array(false, $errors);
} //end of check login function
?>