I'am currently making a login system for news website as part of my coursework. For some reason when I use $rows->num_rows == 1 in an if statement, it always runs the "else" code. Basically this means that it doesnt detect a row in my table that corresponds with the correct user information being inputed... Here is the PHP code that is ran when any information is input into the html form.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//Connect to DB
include_once("db_connect.php")or die ("Couldnt connect to DB");
$username = $_POST['user'];
$password = $_POST['password'];
session_start();
if(trim($username) != '' and trim($password) != ''){
//Sanitizes whatever is entered
$username=stripslashes($username);
$password=stripslashes($password);
$username=strip_tags($_POST['user']);
$password=strip_tags($_POST['password']);
$username=mysqli_real_escape_string($conn,$username);
$password=mysqli_real_escape_string($conn,$password);
//Checks whether Username exists
$query = mysqli_query($conn, "SELECT * FROM user WHERE users='$username'
AND password = '$password' ")
or die(mysqli_error($conn));
$numrows=mysqli_num_rows($query);
if($numrows > 0){
// echo "Record exists.";
$_SESSION['login_user']=$username; // Initializing Session
header("location: index.php"); // Redirecting To Other Page
exit;
}
else {
echo "Username or password is incorrect.";
}
}else{
echo "Please enter information";
}
?>
The problem occurs at the last if statement as it never detects a row. And yes, my table is populated with 1 row of user information (user,password) and my HTML form also uses POST.
I have researched this issue for at least 3 hours and still cant find a resolution.
Here are the current error logs:
Warning: include_once(1): failed to open stream: No such file or directory in /home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 6
Warning: include_once(): Failed opening '1' for inclusion
(include_path='.:/usr/share/pear/') in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 6
Notice: Undefined variable: conn in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 22
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null
given in /home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line
22
Notice: Undefined variable: conn in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 23
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null
given in /home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line
23
Notice: Undefined variable: conn in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 42
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 42
Notice: Undefined variable: conn in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 43
Warning: mysqli_error() expects parameter 1 to be mysqli, null given in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 43
EDIT: Using Fred -ii- answer. include_once("db_connect.php")or die ("Couldnt connect to DB");
has now been moved to the top of the code.
Secondly, a new if statement has been added to replace the older version. This statement can also be found in Fred -ii- answer.
Thirdly, SQL statement has been fixed since I was mixing up the table and column name.
Lastly, error_reporting(E_ALL); ini_set('display_errors', 1);
has been added to help find errors, again courtesy of Fred -ii- answer.