Php 是$this->;函数名之前需要变量才能将数据传输到数据库?
当我从表单向db提交用户数据时,我收到以下错误消息 致命错误:未捕获错误:调用/Applications/XAMPP/xamppfiles/htdocs/Spotifyclone/includes/accounts/Account.php:175堆栈跟踪:#0/Applications/XAMPP/xamppfiles/htdocs/Spotifyclone/includes/handlers/register handlers.php(49):Account->register('Barthewonder','Bartholemewjohn…','Simpson','Bart。simpson@gm...“巴特。simpson@gm...“,”password“,”password“)#1/Applications/XAMPP/xamppfiles/htdocs/spotifyclone/register.php(21):include('/Applications/X..)#2{main}在第175行抛出/Applications/XAMPP/xamppfiles/htdocs/spotifyclone/includes/accounts/Account.php insertUserDetails是所讨论的函数 my account.php文件的第175行如下所示:Php 是$this->;函数名之前需要变量才能将数据传输到数据库?,php,Php,当我从表单向db提交用户数据时,我收到以下错误消息 致命错误:未捕获错误:调用/Applications/XAMPP/xamppfiles/htdocs/Spotifyclone/includes/accounts/Account.php:175堆栈跟踪:#0/Applications/XAMPP/xamppfiles/htdocs/Spotifyclone/includes/handlers/register handlers.php(49):Account->register('Barthe
return insertUserDetails($un, $fn, $ln, $em, $pw);
下面是粘贴的完整代码
<?php
class Account {
private $con;
private $errorArray;
public function __construct($con) {
$this->con = $con;
$this->errorArray = array();
}
public function register($un, $fn, $ln, $em, $em2, $pw, $pw2) {
$this->validateUsername($un);
$this->validateFirstName($fn);
$this->validateLastName($ln);
$this->validateEmails($em, $em2);
$this->validatePasswords($pw, $pw2);
if(empty($this->errorArray) == true) {
//Insert into db
return insertUserDetails($un, $fn, $ln, $em, $pw);
}
else {
return false;
}
}
public function getError($error) {
if(!in_array($error, $this->errorArray)) {
$error = "";
}
return "<span class='errorMessage'>$error</span>";
}
private function insertUserDetails($un, $fn, $ln, $em, $pw) {
$encryptedPw = md5($pw);
$profilePic = "assets/images/profile-pics/head_emerald.png";
$date = date("Y-m-d");
$result = mysqli_query($this->con, "INSERT INTO users VALUES ('', '$un', '$fn', '$ln', '$em', '$encryptedPw', '$date', '$profilePic')");
return $result;
}
private function validateUsername($un) {
if(strlen($un) > 25 || strlen($un) < 5) {
array_push($this->errorArray, Constants::$usernameCharacters);
return;
}
//TODO: check if username exists
}
private function validateFirstName($fn) {
if(strlen($fn) > 25 || strlen($fn) < 2) {
array_push($this->errorArray, Constants::$firstNameCharacters);
return;
}
}
private function validateLastName($ln) {
if(strlen($ln) > 25 || strlen($ln) < 2) {
array_push($this->errorArray, Constants::$lastNameCharacters);
return;
}
}
private function validateEmails($em, $em2) {
if($em != $em2) {
array_push($this->errorArray, Constants::$emailsDoNotMatch);
return;
}
if(!filter_var($em, FILTER_VALIDATE_EMAIL)) {
array_push($this->errorArray, Constants::$emailInvalid);
return;
}
//TODO: Check that username hasn't already been used
}
private function validatePasswords($pw, $pw2) {
if($pw != $pw2) {
array_push($this->errorArray, Constants::$passwordsDoNoMatch);
return;
}
if(preg_match('/[^A-Za-z0-9]/', $pw)) {
array_push($this->errorArray, Constants::$passwordNotAlphanumeric);
return;
}
if(strlen($pw) > 30 || strlen($pw) < 5) {
array_push($this->errorArray, Constants::$passwordCharacters);
return;
}
}
}
?>
insertUserDetails
不是一个常规函数,它是一个类方法。它使用对象的内部属性(这里是连接$this->con
),需要应用到要执行的对象上
$this
指的是当前对象,因此它确实是您案例中的解决方案
这是面向对象编程中的一个基本概念,因此,如果您想使用这些概念,请至少看一下前几章:将来请花时间格式化您的问题内容。您用这段代码实现了一些非常糟糕的做法。MD5哈希(请注意,不是变量名所暗示的加密),SQL中的值/变量,密码仅允许包含字母数字字符。您的validateUsername
与insertUserDetails
相同,不知道为什么要以不同的方式调用它们。永远不要以明文或使用MD5/SHA1存储密码!只存储使用PHP创建的密码哈希,然后您可以验证using.看看这篇文章:了解更多关于md5
不是一种加密方法!它是一个散列函数。感谢Dharman和user3783243!感谢您的指导