0

我不知道为什么这根本不起作用。我的理解可能有误,这就是原因。

这是情况。

  1. MVC 模式
  2. 表单验证的东西

这是代码

public function userExist($data) 
{
    $string = "SELECT student_number FROM users WHERE student_number = :user";
    $sth = $this->db->prepare($string);
    $sth->execute(array(
        ':user' => $data['user']
    ));
    return $sth->rowCount() == 0 ? true : false;
}

public function validate($data) {
    $this->userExist($data);
}

我想要的是返回一个字符串,上面写着“用户存在”,如果userExist方法是假的......但是这段代码不起作用:

if($sth->rowCount() == 0) {
    return true;
} else {
    return "User Already Exists";
}

这就是我如何在控制器中调用它们:

if ($this->model->validate($data) == true) {
    $this->model->create($data);
    header('Location: '.URL.'users');
} else {
    echo $this->model->validate($data);
    die();
}

你认为最好的解决方案是什么?

4

1 回答 1

2

首先,您需要返回 validate 的值:

public function validate($data) {
    $this->userExist($data);
}

但这里还有一些其他问题。您不需要在控制器中调用 $this->model->validate($data) 两次。您可以执行以下操作:

$result = false;
$result = $this->model->validate($data);
if ( true === $result {
    $this->model->create($data);
    header('Location: '.URL.'users');
} else {
    die($result);
}
于 2012-08-03T04:49:58.457 回答