Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The user is not logging in and giving this error

Notice: Undefined variable: connect in C:\xampp\htdocs\stock\php_action\db_connect.php on line 10

Notice: Trying to get property of non-object in C:\xampp\htdocs\stock\php_action\db_connect.php on line 10
Successfully connected
Notice: Undefined variable: connect in C:\xampp\htdocs\stock\index.php on line 26

Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\stock\index.php on line 26


What I have tried:

db_connect code is:
<?php
 $db_name = "stock";
 $mysql_user = "root";
 $mysql_pass = "";
 $server_name = "localhost";
 $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);


//check connection
if($connect->connect_error) {
  die("Connection Failed  " . $connect->connect_error);
} else {
   echo "Successfully connected";
}

?>


and index.php is:

<?php

require_once 'php_action/db_connect.php';

session_start();

if (isset($_SESSION['userId'])) {
	header('location: http://localhost/stock/dashboard.php');
}

$errors = array();

if ($_POST) {
	$username = $_POST['username'];
	$password = $_POST['password'];

	if (empty($username) || empty($password)) {
	if ($username == "") {
		$errors[] = "Username is required";
			}		
			if ($password == "") {
				$errors[] = "Password is required";
			}
	}else {
		$sql = "SELECT * FROM users WHERE username == '$username'";
		$result = $connect->query($sql);

		if($result->num_rows == 1) {
			$password == md5($password);
			// exists
			$mainSql = "SELECT * FROM users WHERE username == '$username' AND password == '$password'";
			$mainResult = $connect->query($mainSql);

			if($mainResult->num_rows == 1) {
				$value = $mainResult->fetch_assoc();
				$user_id = $value['user_id'];

				// set session
				$_SESSION['userId'] = $user_id;

				header('location: http://localhost/stock/dashboard.php');
	}else{
		$errors[] = "Incorrect username or password combination";
	}
}else{
	$errors[] = "Username doesnot exists";
Posted
Updated 9-May-18 2:58am

<?php
 $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);

//check connection
if($connect->connect_error) {
?>


Your variable is called $con but you are checking $connect, which doesn't exist. :)
 
Share this answer
 
Comments
TaimurRiaz 9-May-18 9:13am    
What is the error in next file?
Thnx
[no name] 9-May-18 9:17am    
It's the same error; you're using $connect while your variable is called $con.
1. At line 10 in db_connect.php you try to access the $connect variable that never been defined.
2. At the same line you try to access a property (connect_error) of $connect that undefined, so it is not an object for sure.
3. The same couple of errors in index.php at line 26...

If there is something good abut PHP parser it is the details of the errors...
 
Share this answer
 

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