0

每当数据库中有游戏的标题和平台与提交的游戏相同时,我想检查(在提交到数据库之前)。

我有这个控制器:

    public function addGame($id = null) { // ID is for children games
    $this->load->library(array('images','form_validation'));
    $this->load->helper('form');
    $this->load->model('contrib_model');

    $this->form_validation->set_rules('gameTitle', 'Tytuł gry', 'required|callback_checkGamePlatform');

    if($this->form_validation->run() == FALSE) {
        $data['title'] = 'Dodaj grę';
        $data['genres'] = $this->contrib_model->getGenres();
        $data['platforms'] = $this->contrib_model->getPlatforms();
        $data['developers'] = $this->contrib_model->getDevelopers();

        $this->template->load('template','theme/contribute/addGame',$data);
    } else {
        $data['submit'] = $this->contrib_model->insertGame();; //submits data
        $this->load->view('theme/contribute/emptyPage', $data); //loads view
    }
}

public function checkGamePlatform() {
    $platform = $this->input->post('gamePlatform');
    $game = $this->input->post('gameTitle');

    $q = $this->contrib_model->checkGame($game,$platform);
    echo $q;
    if($q === '0') {
        $this->form_validation->set_message('checkGamePlatform','gra istnieje');
    }
}

而这个模型:

    function checkGame($name,$platform) {
    $q = $this->db->get_where('games',array(
        'name' => $name,
        'plat' => $platform
    ));

    return $q -> num_rows();

}

问题是,它根本不起作用。我做错了什么?

4

2 回答 2

1

如果有相同类型的游戏,您将希望返回错误消息。此外,回调必须返回布尔值。

checkGamePlatform

if ($q > 0) {
     $this->form_validation->set_message('checkGamePlatform','gra istnieje');
     return FALSE;
}
return TRUE;
于 2012-08-06T12:47:24.933 回答
0

改变

if($q === '0')

if($q === 0)

因为num_rows()在与字符串进行比较时将返回整数值。

于 2012-08-06T12:42:21.410 回答