Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么插入方法会跳过验证?_Php_Oop - Fatal编程技术网

Php 为什么插入方法会跳过验证?

Php 为什么插入方法会跳过验证?,php,oop,Php,Oop,我试图在注册用户时练习OOP,所以我开始进行验证和插入。问题是,当我直接从类中调用验证函数时,它正常地执行任务,但是当我调用调用验证的插入函数时,似乎没有发生验证,只是跳过插入 <?php require_once('../dbconnection.php'); class USERS { const usersTable = TABLE_PREIX.'-users'; public $username, $firstname, $lastname, $email,

我试图在注册用户时练习OOP,所以我开始进行验证和插入。问题是,当我直接从类中调用验证函数时,它正常地执行任务,但是当我调用调用验证的插入函数时,似乎没有发生验证,只是跳过插入

    <?php
require_once('../dbconnection.php');
class USERS {
    const usersTable = TABLE_PREIX.'-users';
    public $username, $firstname, $lastname, $email, $password, $error,$_connection;
    public function __construct(){
        //Create DB connection
        $this->_connection= DB_CONN::getInstance()->getConnection();
    }

    //Set user
    public function setUSER($username, $email, $firstname = "", $lastname = ""){
        $this->username = $username;
        $this->email = $email;
        $this->firstname = $firstname;
        $this->lastname = $lastname;

        $this->validateUser($this->username,$this->email,$this->firstname,$this->lastname);

        $sql = "INSERT INTO xjk-users (username, email, firstname, lastname)
                VALUES ($this->username, $this->email, $this->firstname, $this->lastname)";

                if ($this->_connection->query($sql) === TRUE) {
                    header('Location: '.SITE_URL.'?message=user_inserted');
                    exit();
                } else {
                    $this->error[] = 'insert_user_error';
                    header('Location:'.SITE_URL.'?errors='.implode(',',$this->error));
                    exit();
                }

    }

    public function validateUser($username, $email, $firstname , $lastname ){
        if(empty($username) || empty($email)){
            return header('Location:'.SITE_URL.'?errors=empty_fields');
        }

        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            return header('Location:'.SITE_URL.'?errors=invalid_email');
        }
    }

}

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['registration'])){
    $username = $_POST['register-username'];
    $email = $_POST['register-email'];
}
$user = new USERS();
$user->setUSER($username,$email);

标题
不会立即停止代码。它只是设置了一个标题

您的
validateUser
应该返回一些内容,或者您可以使用异常模型并将其抛出无效输入

public function validateUser($username, $email, $firstname, $lastname) {
    if( empty($username) || empty($email)) {
        throw new BadFunctionCallException("Missing name/email");
    }
    if( !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        throw new UnexpectedValueException("Invalid email");
    }
}
然后,调用此函数捕获验证错误时,可以使用
try..catch

或者,只需让函数
返回true
返回false基于参数的有效性


总之,当输入无效时,您需要实际停止插入代码的运行

当调用方法
validateUser()
时,它有一个返回值,它是一个头。调用方法
setUSER()
时,它调用
validateUser()
方法,但对结果不做任何处理。因此,即使
validateUser()
返回一个标头(是的,该标头将构成页面响应的一部分,这就是PHP
header()
函数的工作方式),它仍将继续执行
setUser()
方法的其余部分,因为它没有指示执行需要中断

我建议将
validateUser()
中的代码块嵌入
setUSER()
方法中

我建议按照Niet的建议使用异常



对OOP感兴趣的附言道具。我不认为你的代码一针见血,但你需要参与进来,以学习如何正确利用OOP技术。在您的情况下,您可能需要一个代表用户的
User
对象,以及一个
UserService
处理诸如验证新用户属性和创建新用户之类的事情。

很好的附加值,但有例外谢谢,是的,我确信我并没有实现所有应该实现的事情,当然,我会考虑你的建议,谢谢你。