【问题标题】:how to check duplicate value in form before insert into database如何在插入数据库之前检查表单中的重复值
【发布时间】:2016-07-11 10:44:52
【问题描述】:

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

控制器

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

【问题讨论】:

  • $getgame 里面有什么,可以在这里回复吗?
  • $getgame 从模型 Game_m 中获取数据。在 get_game 函数中,我从数据库桌面游戏中选择数据,以便检查数据库中帖子和数据的值

标签: php activerecord codeigniter-3


【解决方案1】:

对于独特的价值,您可以在codeigniter 中使用is_unique[],如下所示

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      相关资源
      最近更新 更多