[英]MVC php passing model to controller
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? 我有这个登录系统,当我将模型链接到控制器时,控制器无法获取模型变量,例如$ username,$ password等。如何正确处理该模型和控制器? I'm not using any framework just pure php and just following the mvc pattern. 我不使用任何框架,只是纯PHP并遵循mvc模式。
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();
There are many problems in your code. 您的代码中有很多问题。
First: empty($username)
will always return false as you have not defined $username in that function. 第一: empty($username)
将始终返回false,因为您尚未在该函数中定义$ username。
Second: variable $row['username']
in different function you cant access local variable outside. 第二:变量$row['username']
在其他函数中,您无法访问外部变量。
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";
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.