【问题标题】:Uncaught Error: Call to undefined function insertUserDetails() in /Volumes/Datas/Sites/dev/sicgaboma/includes/classes/Account.inc.php:25未捕获的错误:调用 /Volumes/Datas/Sites/dev/sicgaboma/includes/classes/Account.inc.php:25 中未定义的函数 insertUserDetails()
【发布时间】: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];  
    }
}

?>

我尝试用源代码替换整个文件夹,以防我输入错误,但我仍然得到同样的错误。

【问题讨论】:

    标签: php sql database forms


    【解决方案1】:

    经过一个多月的挫折,经过一系列测试,我弄清楚了问题所在。

    下面的sql语句有错误。

    $result = mysqli_query($this->connect, "INSERT INTO users VALUES('', '$uname', '$fname', '$lname', '$eml', '$encryptedPasswd', '$date', '$profilePic')");
    

    当我直接在 phpmyadmin 中执行相同的语句时,我收到一条错误消息,指出 ID 不正确,因此我将空字符串 '' 替换为 '0' 并且它可以工作。

    然后我在 php 代码中做了同样的事情,现在一切都按预期工作了。

    【讨论】:

    • 这不会导致显示的错误。 insertUserDetails 应该是 $this-&gt;insertUserDetails 如果它属于同一个类,或者 $instance-&gt;insertUserDetails 如果它是一个名为 $instance 的类的一部分。
    • 感谢您指出这一点,我确实将 insertUserDetails 更改为 $this->InsertUserDetails 这是我看到的第一个错误,但之后仍然无法将数据插入 mysql 数据库。我想我后来忘记了那部分。 @ceejayoz
    【解决方案2】:
    if(empty($this->errorArray) == true) {// Check if array contain errors, if not insert data into db
                return this-> insertUserDetails($uname, $fname, $lname, $eml, $passwd);
    }
    

    // this-> 插入userdetails函数需要引用

    【讨论】:

    • this -> 需要参考
    猜你喜欢
    • 1970-01-01
    • 2017-02-11
    • 2018-09-22
    • 2017-07-25
    • 2019-04-02
    • 2016-04-09
    • 2020-04-29
    • 2017-07-15
    • 2022-06-14
    相关资源
    最近更新 更多