-1

I'm trying to convert a login function into class with some functions, it was working until I converted it into class, this is my code:

class merwaa_login {
public $conn;
public function __construct($conn) {
    $this->conn = $conn;
    if(isset($_POST['login-submit'])) {
    $username = merwaa_clean($_POST['username']);
    $password = merwaa_clean($_POST['password']);
    $sql = $conn->prepare("SELECT * FROM users WHERE username = ? OR email = ?;");
    $sql->bind_param('ss', $username, $username);
    $sql->execute();
    $all = $sql->get_result();
    $all = $all->fetch_assoc();
    if (count($all) === 0){
        echo 'البيانات المدخلة خاطئة!';
    }
    else{
        $hashed_pwd = $all['password'];
        if (password_verify($password,$hashed_pwd)) {
            echo 'كفو';
            $_SESSION['username'] = $username = $all['username'];
            if(isset($POST['rememberme'])) {
                $uid = $all['ID'];
                merwaa_set_jwt($username,$uid);
            }

        } else{
            echo 'خطأ';
        }
    }
}
}
}

May I've some grammar mistakes, please notice me about it.

Maometto
  • 84
  • 3
  • 13

2 Answers2

0

Change this :-

$sql = $conn->prepare("SELECT * FROM users WHERE username = ? OR email = ?;");

To this :-

$sql = $this->conn->prepare("SELECT * FROM users WHERE username = ? OR email = ?;");

Or, you could change public $conn to global $conn. Then you won't need to make the above change.

Akshay
  • 2,244
  • 3
  • 15
  • 34
0

try to use $this->conn->prepare instead of $conn->prepare and a proper connection object should be passed to the constructor

haseeb
  • 682
  • 5
  • 16