I am facing an error

Hi to all
I have this code after execution I am facing this error

public function adminLogin(){		
	$errorMessage = '';
	if(!empty($_POST["login"]) && $_POST["email"]!=''&& $_POST["password"]!='') {	
		$email = $_POST['email'];
		$password = $_POST['password'];
		$sqlQuery = "SELECT * FROM ".$this->userTable." 
			WHERE email='".$email."' AND password='".md5($password)."' AND status = 'active' AND type = 'administrator'";
	70           $resultSet = mysqli_query($this->dbConnect, $sqlQuery) or die("error".mysql_error());
	71    	$isValidLogin = mysqli_num_rows($resultSet);	
		if($isValidLogin){
			$userDetails = mysqli_fetch_assoc($resultSet);
			$_SESSION["adminUserid"] = $userDetails['id'];
			$_SESSION["admin"] = $userDetails['first_name']." ".$userDetails['last_name'];
			header("location: dashboard.php"); 		
		} else {		
			$errorMessage = "Invalid login!";		 
		}
	} else if(!empty($_POST["login"])){
		$errorMessage = "Enter Both user and password!";	
	}
	return $errorMessage; 		
}

Fatal error : Uncaught mysqli_sql_exception: No database selected in C:\xampp\htdocs\school-management-system-php-mysql-demo\class\School.php:70 Stack trace: #0 C:\xampp\htdocs\school-management-system-php-mysql-demo\class\School.php(70): mysqli_query(Object(mysqli), ‘SELECT * FROM s…’) #1 C:\xampp\htdocs\school-management-system-php-mysql-demo\index.php(8): School->adminLogin() #2 {main} thrown in C:\xampp\htdocs\school-management-system-php-mysql-demo\class\School.php on line 70

Welcome aboard!

That code is why I hate the internet at times as I am assuming you found an old tutorial on how to write to an MySQL database table? :thinking:

It also looks like you pieced the script using mysqli at least that is a start.

My suggestion is to throw that script in file 13, use PDO instead of mysqli ( A good reference on PDO - (The only proper) PDO tutorial - Treating PHP Delusions), use password_hash(PHP: password_hash - Manual) and password_verify(PHP: password_verify - Manual) instead of md5 and use prepared statements.

One last thing put you code between the </> tags as it makes it easier for people to read your code.

Example:

    public function login(): void
    {
        $sql = "SELECT id, hashed_password FROM " . static::$table . " WHERE username =:username LIMIT 1";

        $user = static::fetch_by_column_name($sql);

        if ($user && password_verify($this->password, $user['hashed_password'])) {
            unset($this->password, $user['hashed_password']);
            session_regenerate_id(); // prevent session fixation attacks
            static::$last_login = $_SESSION['last_login'] = time();
            $this->id = $_SESSION['id'] = $user['id'];
            header("Location: admin.php");
            exit();
        }

        static::$error[] = 'Unable to login in!';

    }

The error is fairly self-explanatory. There’s no database selected when you try to run the query. When you make the database connection, you should almost always select the (default) database to use. This is a parameter in the mysqli or PDO connection statement. When you converted the old mysql code to use mysqli, it had a separate database select statement. There is an equivalent mysqli select database statement, but it is much simpler to just include the database parameter in the connection statement.

Thanks, Dear sir. It helps me a lot.
warm regads

Sponsor our Newsletter | Privacy Policy | Terms of Service