如何在插入数据库之前检查表单中的重复值

how to check duplicate value in form before insert into database

I would like to check duplicate data when i insert duplicate in textbox it will display duplicate and I tried to insert not duplicate it display "Trying to get property of non-object"

Controller

public function gamecheck(){
    $this->load->library('form_validation'); // load from validation
    $this->form_validation->set_rules('gamename', 'Checkgamename', 'required');
    if ($this->form_validation->run() == TRUE) {
    if($this->input->post()) {
        $this->load->model('game_m');
        $gamepost = $this->input->post('gamename');
        $getgame = $this->game_m->get_game($gamepost);

           if($getgame->gamename!==''){
            echo "duplicate";
           }else{
            echo "not duplicate";
           }
         }
      }
    $this->load->view('header');
    $this->load->view('menu');
    $this->load->view('game/gamecheck');
    $this->load->view('footer');
}

Model

class Game_m extends CI_model{
public function get_game($gamepost) {
     $this->db->from('game');
     $this->db->where('gamename',$gamepost);
     return $this->db->get()->row();
    }
}

对于唯一值,您可以在 codeigniter 中使用 is_unique[],如下所示

$this->form_validation->set_rules('fieldname', 'Msg you want to show', 'required|is_unique[table.coloum]');

对于唯一值,我在 codeigniter 中使用 is_unique[]。

$this->form_validation->set_rules('gamename', 'Field is required', 'required|is_unique[game.gamename]');