1

I am trying to create a login script that collects the information a user posts and then creates a session if the user has provided the relevant information. Every time I try to login I get the error:

PHP Fatal error: Call to a member function bind_param() on boolean

I have provided my code below: require '../../../db-config.php'; require '../distributor-config.php';

$user_role = $_POST['user_role'];
$user_username = $_POST['user_username'];
$user_password = $_POST['user_password'];

$stmt = $conn->prepare("SELECT role, id, emailaddress, username, firstname, surname FROM users WHERE role = ?, username = ?, password = ?");
$stmt->bind_param("sss", $user_role, $user_username, $user_password);
$stmt->execute();

$stmt->bind_result($role, $id, $emailaddress, $username, $firstname, $surname);

while($stmt->fetch()) {
    if($stmt == 1 && $user_role == 'Manager') {
        session_start();
        $_SESSION['userinfo']['role'] = $id;
        $_SESSION['userinfo']['id'] = $id;
        $_SESSION['userinfo']['emailaddress'] = $emailaddress;
        $_SESSION['userinfo']['username'] = $username;
        $_SESSION['userinfo']['firstname'] = $firstname;
        $_SESSION['userinfo']['surname'] = $surname;

        header('location: ../manager-index.php?page=login&status=success');
    }

    elseif($stmt == 1 && $user_role == 'User') {
        session_start();
        $_SESSION['userinfo']['role'] = $id;
        $_SESSION['userinfo']['id'] = $id;
        $_SESSION['userinfo']['emailaddress'] = $emailaddress;
        $_SESSION['userinfo']['username'] = $username;
        $_SESSION['userinfo']['firstname'] = $firstname;
        $_SESSION['userinfo']['surname'] = $surname;

        header('location: ../user-index.php?page=login&status=success');
    }

    else {
        header('location: ../login.php?page=login&status=error');
    }
}

$stmt->close();
$conn->close();

3 Answers3

0

The Problem is in the SQL-Statement. If the SQL fails, it returns FALSE

See: stackoverflow.com

$stmt = $conn->prepare("SELECT role, id, emailaddress, username, firstname, surname FROM users WHERE role = ? and username = ? and password = ?");
Community
  • 1
  • 1
drPhilip
  • 96
  • 4
0

This is wrong :

$stmt = $conn->prepare("SELECT role, id, emailaddress, username, firstname, surname FROM users WHERE role = ?, username = ?, password = ?");

It should be :

$stmt = $conn->prepare("SELECT role, id, emailaddress, username, firstname, surname FROM users WHERE role = ? AND username = ? AND  password = ?");
Masivuye Cokile
  • 4,729
  • 3
  • 18
  • 34
0

Look this place remove comma , add and in where condition

$stmt = $conn->prepare("SELECT role, id, emailaddress, username, firstname, surname FROM users WHERE role = ? AND username = ? AND  password = ?");

Docs refer the mysqli prepared statement

Note:

1) session_start(); should be once and also it should be in page top most place

JYoThI
  • 11,793
  • 1
  • 10
  • 25