解决方案 是
hashed_password varchar(40) NOT NULL,
我更改为
hashed_password varchar(255) NOT NULL,
我不知道为什么,我无法将哈希密码插入数据库(MySql)。 当我使用hashed_password时,数据库中没有记录。
有效-$this->hashed_password = $this->password;
它不起作用-$this->hashed_password = password_hash($this->password, PASSWORD_BCRYPT);
功能创建管理员
public function create_admin() {
$this->hashed_password = password_hash($this->password, PASSWORD_BCRYPT);
//$this->hashed_password = $this->password;
$sql = "INSERT INTO " . self::$table_name . " (";
$sql .= "first_name, last_name, email, username, hashed_password";
$sql .= ") VALUES ('";
$sql .= self::$database->escape_string($this->first_name) ."', '";
$sql .= self::$database->escape_string($this->last_name) ."', '";
$sql .= self::$database->escape_string($this->email) ."', '";
$sql .= self::$database->escape_string($this->username) ."', '";
$sql .= self::$database->escape_string($this->hashed_password) ."')";
echo $sql;
$result = self::$database->query($sql);
if($result) {
$this->id = self::$database->insert_id;
}
return $result;
}
输出sql
INSERT INTO admins (first_name, last_name, email, username, hashed_password) VALUES ('John', 'Smith', 'js@gmail.com', 'neo11', '$2y$10$MhWRX8Ierh6Kk19anqBIEOsx7Rlfk9gBpD/eQbSmXbVWEkqf64gaS')