php 和 c-panel 令人沮丧,用户似乎没有正确注册到数据库中,我收到错误

标签 php mysql sql

每当我出于某种原因尝试注册用户时,C 面板都会给出以下错误,我的代码有什么问题,请帮忙!!

注意: undefined variable :/home/sultanp1/public_html/includes/handlers/register-handler.php 第 32 行中的帐户

fatal error :未捕获错误:在/home/sultanp1/public_html/includes/handlers/register-handler.php:32 中对 null 调用成员函数 register() 堆栈跟踪:#0/home/sultanp1/public_html/register.php(5): include() #1 {main} 在第 32 行的/home/sultanp1/public_html/includes/handlers/register-handler.php 中抛出

注册处理程序.php

<?php

function sanitizeFormPassword($inputText){
    $inputText = strip_tags($inputText);
    return $inputText;
}

function sanitizeFormUsername($inputText){
    $inputText = strip_tags($inputText);
    $inputText = str_replace(" ", "", $inputText);
    return $inputText;
}

function sanitizeFormString($inputText){
    $inputText = strip_tags($inputText);
    $inputText = str_replace(" ", "", $inputText);
    $inputText = ucfirst(strtolower($inputText));
    return $inputText;
}


if(isset ($_POST['registerButton'])){
   //Register button was pressed
    $username = sanitizeFormUsername ($_POST['username']);
    $firstName = sanitizeFormString ($_POST['first_name']);
    $lastName = sanitizeFormString ($_POST['last_name']);
    $email = sanitizeFormString ($_POST['email']);
    $confirmEmail = sanitizeFormString ($_POST['confirmEmail']);
    $password = sanitizeFormPassword ($_POST['password']);
    $confirmPassword = sanitizeFormPassword ($_POST['confirmPassword']);

    $wasSuccessful = $account->register($username, $first_name, $last_name, $email, $password);

    if($wasSuccessful){
        $_SESSION['userLoggedIn'] = $username;
        header("Location: home.php");

    }
}
?>

注册.php

<?php
require_once("../resources/config.php"); 
include("includes/classes/Account.php");
include("includes/classes/Constants.php");
include("includes/handlers/register-handler.php");
include("includes/handlers/login-handler.php");
$account = new Account($connection);


function getInputValue ($name){
    if(isset($_POST[$name])){
        echo $_POST[$name];
    }
}
?>

<html>
<head>
    <title>Welcome to Music Streaming Service</title>
    <link rel="stylesheet" type="text/css" href="assets/css/register.css">

</head>
    <body>
    <div id="background">
        <div id="loginContainer">
            <div id="inputContainer">
            <form id="loginForm" action="register.php" method="post"> 
                <h2>Login to your account</h2>

                <p>
                    <?php echo $account->getError(Constants::$loginFailed); ?>
                    <label for="loginUsername">Username</label>
                    <input id="loginUsername" name="loginUsername" type="text" placeholder="e.g. Smith01" required></p>
                <p>
                <label for="loginPassword">Password</label>
        <input id="loginPassword" name="loginPassword" type="password" required>
                </p>

                <button type="submit" name="loginButton">Login</button>
                <div class="hasAccountText">
                <span id="hideLogin">Don't have an Account yet? Signup Here.</span>
                </div>
                </form>


            <form id="registerForm" action="register.php" method="post"> 
                <h2>Create your free account</h2>

                <p>
                    <?php echo $account->getError(Constants::$usernameCharacters); ?>
                    <?php echo $account->getError(Constants::$usernameTaken); ?>
                    <label for="registerUsername">Username</label>
                    <input id="registerUsername" name="username" type="text" placeholder="e.g. Smith01" required value="<?php getInputValue('username') ?>"> 
                </p>
                 <p>
                    <?php echo $account->getError(Constants::$firstNameCharacters); ?>
                    <label for="firstName">First Name</label>
                    <input id="firstName" name="first_name" type="text" placeholder="e.g. John" required value= "<?php getInputValue('first_name') ?>" >
                </p>

                 <p>
                    <?php echo $account->getError(Constants::$lastNameCharacters); ?>
                    <label for="lastName">Last Name</label>
                    <input id="lastName" name="last_name" type="text" placeholder="e.g. Smith" required value="<?php getInputValue('last_name') ?>">
                </p>

                 <p>
                    <?php echo $account->getError(Constants::$emailsDoNotMatch); ?>
                    <?php echo $account->getError(Constants::$emailInvalid); ?>
                    <?php echo $account->getError(Constants::$emailTaken); ?>
                    <label for="email">E-mail </label>
                    <input id="email" name="email" type="email" placeholder="e.g. johnsmith@email.com" required value="<?php getInputValue('email') ?>">
                </p>

                 <p>
                    <label for="confirmEmail">Confirm Email</label>
                    <input id="confirmEmail" name="confirmEmail" type="email" required value="<?php getInputValue('confirmEmail') ?>">
                </p>
                 <p>
                    <?php echo $account->getError(Constants::$passwordsDoNoMatch); ?>
                    <?php echo $account->getError(Constants::$passwordsNotAlphanumeric); ?>
                    <?php echo $account->getError(Constants::$passwordCharacters); ?>
                    <label for="password">Password</label>
                    <input id="password" name="password" type="password" required>
                </p>
                <p>
                <label for="confirmPassword">Confirm Password</label>
        <input id="confirmPassword" name="confirmPassword" type="password" required>
                </p>

                <button type="submit" name="loginButton">Login</button>
                <button type="submit" name="registerButton">Sign-Up</button>
                <div class="hasAccountText">
                <span id="hideRegister">Already have an Account? Login Here.</span>
                </div>
                </form>



                </div>
            </div>
        </div>
    </body>
</html>

Account.php - 可能不需要这个来帮助我

<?php

    class Account {
        private $connection;
        private $errorArray;

    public function __construct($connection){
                $this->con = $connection;
                $this->errorArray = array();
            }
    public function login($un, $pw){
        $pw = md5($pw);

        $query = mysqli_query($this->con, "SELECT * FROM customers WHERE username='$un' AND password='$pw'");
        if(mysqli_num_rows($query)== 1){
            return true;
        }
        else{
            array_push($this->errorArray, Constants::$loginFailed);
            return false;
        }
    }

    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)){
            //Insert into db
            return $this->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/generic-profile";
        $date = date("Y-m-d");

        $result = mysqli_query($this->con, "INSERT INTO customers VALUES ('', '$un', '$fn', '$ln', '$em', '$encryptedPw')");
        return $result;
    }     

    private function validateUsername($un){
        if(strlen($un) > 25 || strlen($un) < 5){
            array_push($this->errorArray, Constants::$usernameCharacters);
            return;
        }

        $checkUsernameQuery = mysqli_query($this->con, "SELECT username FROM customers WHERE username = '$un' ");
        if(mysqli_num_rows($checkUsernameQuery) != 0){
            array_push($this->errorArray, Constants::$usernameTaken);
            return;
        }
    }
    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;
        }

       $checkEmailQuery = mysqli_query($this->con, "SELECT email FROM customers WHERE email = '$em' ");
        if(mysqli_num_rows($checkEmailQuery) != 0){
            array_push($this->errorArray, Constants::$emailTaken);
            return;
        }


    }

    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::$passwordsNotAlphanumeric);
                return;
            }
         if(strlen($pw) > 30 || strlen($pw) < 5){
            array_push($this->errorArray, Constants::$passwordCharacters);
            return;
        }
    }


        }



?>

最佳答案

您未能在页面/handlers/register-handler.php 第 32 行之前创建类 Account 的对象:

if(isset ($_POST['registerButton'])){
    ....
    $account = new Account($connectionObject); /* missing line, use appropriate name for connectionObject */
    $wasSuccessful = $account->register($username, $first_name, $last_name, $email, $password);

关于php 和 c-panel 令人沮丧,用户似乎没有正确注册到数据库中,我收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50195427/

相关文章:

PHP 复选框会更新数据库,但下拉菜单不会更新

mysql - ServiceStack.OrmLite : Implementing custom StringConverter affects column type of complex BLOB fields

MySQL 同一单元上的多个 JOINS

java - 处理超出需要的属性

sql - PostgreSQL:间隔 '10 DAY' 和当前行之间的范围

php - 我如何在cakephp中使用插件主题来显示错误消息?

php - session 在新页面 View 中被破坏

php - 使用 php 的 Sharepoint soap

mysql - 从 minikube 访问在本地主机上运行的 mysql

MySQL:START TRANSACTION - UPDATE - ROLLBACK:非事务表