每当数据库中有游戏的标题和平台与提交的游戏相同时,我想检查(在提交到数据库之前)。
我有这个控制器:
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();
}
问题是,它根本不起作用。我做错了什么?