When I run script I get error
Fatal error: Uncaught Error: Call to a member function prepare() on string in /Applications/MAMP/htdocs/sae/TT/public_html/includes/user.php:21 Stack trace: #0 /Applications/MAMP/htdocs/sae/TT/public_html/includes/user.php(40): User->emailExists('user1@gmail.com') #1 /Applications/MAMP/htdocs/sae/TT/public_html/includes/user.php(102): User->createUserAccount('userr', 'user1@gmail.com', '123456789', 'Admin') #2 {main} thrown in /Applications/MAMP/htdocs/sae/TT/public_html/includes/user.php on line 21
Please have a look in code and help me find out error.
<?php
ini_set("display_errors", 1); error_reporting(E_ALL);
class User
{
private $con;
function __construct()
{
include_once("../database/db.php");
$db = new Database();
$this->con = $db->connect();
// if ($this->con) {
// echo "COnnected";
// }
}
// Check user
private function emailExists($email)
{
$pre_stmt=$this->con->prepare("SELECT id FROM user WHERE email = ? ");
$pre_stmt->bind_param("s", $email);
$pre_stmt->execute() or die($this->con->error);
$result = $pre_stmt->get_result();
if ($result->num_rows>0) {
return 1;
} else
{
return 0;
}
}
public function createUserAccount($username,$email,$pass_hash,$usertype)
{
if ($this->emailExists($email)) {
return "EMAIL_ALREADY_EXISTS";
}else{
$pass_hash = password_hash($password,PASSWORD_BCRYPT,["cost"=>8]);
$date = date("Y-m-d");
$notes = "";
$pre_stmt = $this->con->prepare("INSERT INTO `user`(`username`, `email`, `password`, `usertype`, `register_date`, `last_login`, `notes`) VALUES (?,?,?,?,?,?,?)");
$pre_stmt->bind_param("sssssss", $username, $email, $pass_hash, $usertype, $date, $date, $notes);
$result = $pre_stmt->execute() or die($this->con->error);
if ($result) {
return $this->con->insert_id;
} else {
return "SOME_ERROR";
}
}
}
public function userLogin($email,$password)
{
$pre_stmt = $this->con->prepare("SELECT id,username,password,last_login FROM user WHERE email =? ");
$pre_stmt->bind_param("s",$email);
$pre_stmt->execute() or die($this->con->error);
$result = $pre_stmt->get_result();
if ($result->num_rows <1) {
return "NOT_REGISTERD";
}else{
$row = $result->fetch_assoc();
if (password_verify($password,$row['password'])) {
$_SESSION['userid'] = $row['id'];
$_SESSION['username'] = $row['username'];
$_SESSION['last_login'] = $row['last_login'];
// updating user last_login
$last_login = date("y-d-m h:m:s");
$pre_stmt = $this->con->prepare("UPDATE user SET last_login = ? WHERE email = ? ");
$pre_stmt->bind_pram("ss",$last_login,$email);
$result = $pre_stmt->execute() or die($this->con->error);
if ($result) {
return 1;
}else{
return 0;
}
}else{
return "PASSWORD_NOT_MATCHED";
}
}
}
}
$user = new User();
echo $user->createUserAccount("userr", "user1@gmail.com", "123456789", "Admin");
echo $user->userLogin("user1@gmail.com","123456789");
?>