MVC php passing model to controller

家住魔仙堡 提交于 2020-07-22 06:25:48

问题


I have this login system, when I link the model to my controller, the controller can't get the variables of model which is $username, $password etc. How to properly way of handling this model and controller? I'm not using any framework just pure php and just following the mvc pattern.

When I run the code it goes to Invalid Username or Password

model

require_once("db.php");

class loginModel{

    public function __construct(){
        $dbCon = new DbConnector();
        $this->dbCon = $dbCon->getConnection();
   }

    public function queryUser($username, $password){ // use parameters
        $username = mysqli_real_escape_string($this->dbCon, $username);
        $password = mysqli_real_escape_string($this->dbCon, sha1($password));
        $empty = "da39a3ee5e6b4b0d3255bfef95601890afd80709";

        $myQuery = "SELECT * FROM login WHERE username = '".$username."' and password = '".$password."'";
        $results = $this->dbCon->query($myQuery);
        return $results->fetch_array(); // return the result 

        if(empty($username) or $password == $empty){
            return "Username or Password is empty";
        }elseif($username == $row['username'] and $password == $row['password']){
        if($results->num_rows == 1){
            session_start();
            $_SESSION['user'] = $username;
            header('Location: ../view/index.php');
        }else{
            return "Login Unsuccessful";
        }
        }else{
            return "Invalid Username or Password";
        }
    }

}

controller

require_once("../model/loginModel.php");

class loginController{
    public $loginModel;
    public function __construct(){  
    $loginModel = new loginModel(); // create object of model
}

    public function select(){
        $userData = $this->loginModel->queryUser($_POST['username'], $_POST['password']);

        if($userData) {
            session_start();
            $_SESSION['user'] = $userData;
            header('Location: ../view/index.php');
        } else {
            return "Invalid Username or Password";
        }
   }
}
$loginController = new loginController();
$loginController->select();

回答1:


There are many problems in your code.

First: empty($username) will always return false as you have not defined $username in that function.

Second: variable $row['username'] in different function you cant access local variable outside.

require_once("db.php");

class loginModel{

    public function __construct(){
        $dbCon = new DbConnector();
        $this->dbCon = $dbCon->getConnection();
    }

    public function queryUser($username, $password){ // use parameters
        $username = mysqli_real_escape_string($this->dbCon, $username);
        $password = mysqli_real_escape_string($this->dbCon, sha1($password));
    $empty = "da39a3ee5e6b4b0d3255bfef95601890afd80709";

        $myQuery = "SELECT * FROM login WHERE username = '".$username."' and password = '".$password."'";
        $results = $this->dbCon->query($myQuery);
        return $results->fetch_array(); // return the result 
      // do the validation and exception handling too
    }
}

Controller:

//require_once("../model/db.php"); No need to imprt this 
require_once("../model/loginModel.php");

class loginController{
    $loginModel;
    public function __construct(){  
        $loginModel = new loginModel(); // create object of model
    }

    public function select(){
        $userData = $this->loginModel->queryUser($_POST['username'], $_POST['password']);

        if($userData) {
            session_start();
                $_SESSION['user'] = $userData;
                header('Location: ../view/index.php');
        } else {
            return "Invalid Username or Password";
        }
   }
}


来源:https://stackoverflow.com/questions/41776177/mvc-php-passing-model-to-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!