【发布时间】:2018-11-05 20:41:27
【问题描述】:
我目前正在关注一组关于登录表单的视频教程,这些视频教程将数据存储在 mysql db 中。我能够连接到数据库,但是当调用这个私有函数来插入 userDetails 时:
private function insertUserDetails($uname, $fname, $lname, $eml, $passwd) {
$encryptedPasswd = md5($passwd);
$profilePic = "assets/img/profile-pics/profile_pic.pgn";
$date = date("Y-m-d");
$result = mysqli_query($this->connect, "INSERT INTO users VALUES('', '$uname', '$fname', '$lname', '$eml', '$encryptedPasswd', '$date', '$profilePic')");
return $result;
}
这里
public function register($uname, $fname, $lname, $eml, $cemail, $passwd, $cpasswd) {
$this->validateUsername($uname);
$this->validateFirstname($fname);
$this->validateLastname($lname);
$this->validateEmail($eml, $cemail);
$this->validatePasswords($passwd, $cpasswd);
if(empty($this->errorArray) == true) {// Check if array contain errors, if not insert data into db
return insertUserDetails($uname, $fname, $lname, $eml, $passwd);
}
else {
return false;
}
}
我收到此错误
致命错误:未捕获的错误:调用 /Volumes/Datas/Sites/dev/sicgaboma/includes/classes/Account.inc.php:25 中的未定义函数 insertUserDetails() 堆栈跟踪:#0 /Volumes/Datas/Sites /dev/sicgaboma/includes/handlers/register-handler.php(35): Account->register('testUser', 'John', 'Doe', 'Johndoe@test.co...', 'Johndoe@test .co...', '1234567', '1234567') #1 /Volumes/Datas/Sites/dev/sicgaboma/register.php(11): include('/Volumes/Datas/...') #2 {main} 在第 25 行的 /Volumes/Datas/Sites/dev/sicgaboma/includes/classes/Account.inc.php 中抛出
该错误与 register-handler.php 中函数 register() 的调用有关。
当我将 return 设置为 TRUE 时,没有错误消息。
代码使用函数__construct连接到sql。
这是目前为止的代码。
class Account {
private $connect; //sql connect
private $errorArray; // Array to contain error messages
public function __construct($connect) {
$this->connect = $connect;
$this->errorArray = array();
}
// VALIDATION FUNCTIONS, these functions are used to validate user input, things like password, email match
public function register($uname, $fname, $lname, $eml, $cemail, $passwd, $cpasswd) {
$this->validateUsername($uname);
$this->validateFirstname($fname);
$this->validateLastname($lname);
$this->validateEmail($eml, $cemail);
$this->validatePasswords($passwd, $cpasswd);
if(empty($this->errorArray) == true) {// Check if array contain errors, if not insert data into db
return insertUserDetails($uname, $fname, $lname, $eml, $passwd);
}
else {
return false;
}
}
这是我认为错误来自的部分
if(isset($_POST['register-button'])){
//Get user info
$username = sanitizeFormUsername($_POST['username']);
$firstname = sanitizeFormString($_POST['firstname']);
$lastname = sanitizeFormString($_POST['lastname']);
$email = sanitizeFormString($_POST['email']);
$confirmEmail = sanitizeFormString($_POST['confirmEmail']);
$password = sanitizeFormPassword($_POST['password']);
$confirmPassword = sanitizeFormPassword($_POST['confirmPassword']);
//$wasSuccessful hold the result(value) of the function call (register);
$wasSuccessful = $account->register($username, $firstname, $lastname, $email, $confirmEmail, $password, $confirmPassword);
if($wasSuccessful == true) {
header("Location: index.php"); //redirect user to index.php if successful.
}
}
这里是我包含所有 php 代码的地方
<!doctype html>
<?php
include("includes/config.php");
include("includes/classes/Account.inc.php");
include("includes/classes/Constants.inc.php");
$account = new Account($connect);
include("includes/handlers/register-handler.php");
include("includes/handlers/login-handler.php");
function getInputValue($name) {
if(isset($_POST[$name])) {
echo $_POST[$name];
}
}
?>
我尝试用源代码替换整个文件夹,以防我输入错误,但我仍然得到同样的错误。
【问题讨论】: