我已经在这里阅读了这些问题和答案,但是这些并没有解决我的问题,我尝试了超过19HRS的速度,我没有找到任何解决方法
注意
php版本:PHP 7.2.15-0ubuntu2(cli)
Fatal error: Uncaught Error: Call to a member function prepare() on string
Fatal error: Uncaught Error: Call to a member function format() on boolean
Fatal error: Uncaught Error: Call to a member function prepare() on null
Uncaught Error: Call to a member function prepare()
Call to a member function prepare() on null - After I upgrade to PHP 7 (duplicate)
这是我的代码
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/**
* user class for account creation and login purpose
*/
class User {
private $con;
function __construct(){
include_once("../database/db.php");
$db = new Database();
$this->con = $db->connect();
}
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,$password,$usertype){
//To protect your application from sql attack you can user
//prepares statment
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";
}
}
}
}
$user = new User();
echo $user->createUserAccount("test","test1@mail.com","12345678","Admin");
?>
我尝试在此处向我的数据库中插入数据,我确认我的数据库连接和表都正确
这是我的错误
Fatal error: Uncaught Error: Call to a member function prepare() on boolean in /var/www/html/inv/includes/user.php:18 Stack trace: #0 /var/www/html/inv/includes/user.php(33): User->emailExists('test1@mail.com') #1 /var/www/html/inv/includes/user.php(54): User->createUserAccount('test', 'test1@mail.com', '12345678', 'Admin') #2 {main} thrown in /var/www/html/inv/includes/user.php on line 18
这是我的db.php
<?php
/**
*
*/
class Database
{
private $con;
public function connect(){
include_once("constants.php");
$this->con = new Mysqli(HOST,USER,PASS,DB);
if ($this->con) {
return $this->con;
}
return "DATABASE_CONNECTION_FAIL";
}
}
$db = new Database();
$db->connect();
?>
这是我的SQL
INSERT INTO `user`(`id`, `username`, `email`, `password`, `usertype`, `register_date`, `last_login`, `notes`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7],[value-8])