如何在插入数据库

时间:2016-07-11 10:44:53

标签: php activerecord codeigniter-3

  

我想检查重复数据,当我在文本框中插入重复它将显示重复,我试图插入不重复显示“试图获取非对象的属性”

     

控制器

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');
}
  

模型

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();
    }
}

2 个答案:

答案 0 :(得分:-1)

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

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

答案 1 :(得分:-1)

表示我在codeigniter中使用is_unique []的唯一值。

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