【发布时间】:2020-12-04 13:11:32
【问题描述】:
这是我的代码,我遇到了这个错误
在 null in 上调用成员函数 prepare() 我认为我的课程没问题,但有一个错误,我不知道如何让这段代码运行
<?php
/*$servername = "localhost:3307";
$username = "root";
$password = "";
$dbname = "motorway";*/
class BaseConnect {
private $conn;
function __construct() {
}
// Create connection
function connect() {
$conn = new mysqli("localhost:3307", "root", "", "motorway");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $this->conn;
}
}
class Database {
private $conn;
function __construct() {
$db = new BaseConnect();
$this->conn = $db->connect();
}
function Enterdata($name, $phone, $email, $password) {
$pass = md5($password);
$sql = $this->conn->prepare("INSERT INTO 'user'('name', 'phone', 'email', 'pass')
VALUES(?, ?, ?);");
$sql->bind_param("ssss", $name, $phone, $email, $pass);
if ($sql->execute()) {
return 1;
} else {
return 2;
}
}
}
【问题讨论】:
-
return $this->conn应该是return $conn -
稍后的问题将是您的 SQL 无效。不要引用标识符,您定义了 4 列,但
VALUES(?, ?, ?)中只有 3 个值。也不应该使用md5,php.net/manual/en/function.password-hash.php -
请注意
BaseConnect类是完全多余的,你应该删除它。您也没有正确使用 mysqli。请启用错误报告。 How to get the error message in MySQLi? -
切勿以明文形式或使用 MD5/SHA1 存储密码! 仅存储使用 PHP 的
password_hash()创建的密码哈希,然后您可以使用password_verify()进行验证。看看这个帖子:How to use password_hash 并了解更多关于bcrypt & password hashing in PHP