使用Codeigniter防止重复输入php

时间:2017-08-14 04:15:19

标签: php codeigniter validation duplicates

我试图阻止带有codeigniter的重复条目,但它不再起作用了。

尽管名称相同,数据仍会插入数据库。

这是控制器代码

   public function add(){
        $title['title']='Add New';
                $data['data']='Insert New Group';
                $data['exist']= 'This Group already exists';
                $this->load->view('add_pbk_g', $data);
    }

    public function save(){
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('namegroup', 'Name', 'trim|required|is_unique[pbk_groups.Name]');
        if($this->form_validation->run()==false){
            $this->add_pbk_g($Name);
        } else{
            $this->db->insert('pbk_groups',array('Name'=> $this->input->post('namegroup')));

            $this->load->model('mpbk_grup');
            if($this->mpbk_grup->check_grup_exist('$Name')){
                $title['title']='Add New';
                $data['data']='Insert New Group';
                $data['exist']= 'This Group already exists';
                $this->load->view('layout/header', $title);
                $this->load->view('add_pbk_g');
                $this->load->view('layout/footer');
            } else{
                $this->mpbk_grup->add;
                redirect('cpbk_grup/index');   
            }
        }
    }

这是模特..

    function add($data){
        return $this->db->create('pbk_groups', $data);
    }

    function check_grup_exist($namegroup){
        $this->db->where('Name', $namegroup);
        $this->db->from('pbk_groups');
        $query = $this->db->get();
        if($query->num_rows() >0){
            return $query->result();
        } else {
            return $query->result();
            //return False;
        }
    }

这是视图

<form method="post" class="form-horizontal" action="<?php echo site_url('cpbk_grup/save');?>">
              <div class="box-body">
                <?php echo validation_errors(); ?>
                <div class="form-group">
                  <label class="col-sm-2 control-label">Nama Group</label>

                  <div class="col-sm-10">
                    <input type="text" name="namegroup" required="" class="form-control" placeholder="Nama Group">
                  </div>
                </div>
              </div>
              <!-- /.box-body -->
              <div class="box-footer">
                <button type="submit" name="submit" value="submit" class="btn btn-info pull-right">Save</button>
                <a href="<?php echo base_URL(); ?>index.php/cpbk_grup" class="btn btn-info pull-right"> Kembali</a>
              </div>
              <!-- /.box-footer -->
            </form>

3 个答案:

答案 0 :(得分:1)

public function save(){
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('namegroup', 'Name', 'trim|required|is_unique[pbk_groups.Name]');
        if($this->form_validation->run()==false){
            $this->add_pbk_g($Name);
        } else{
            $data = array ('Name' => $this->input->post('namegroup'));
            $this->mpbk_grup->check_grup_exist($data);
        }
    }

答案 1 :(得分:0)

此行上的$ Name来自哪里?

if($this->mpbk_grup->check_grup_exist('$Name')){

它也不应该有引号,应该是......

if($this->mpbk_grup->check_grup_exist($Name)){

答案 2 :(得分:0)

尝试在保存功能中执行此操作

Controller Code:
$data = array('Name' => $this->input->post('namegroup'));
$this->Model_Name->check_grup_exist($data);

我的模特

Model_Name


$this->main_table = 'pbk_groups';
 public function check_grup_exist($data)
{
 $query = $this->db->get_where($this->main_table, array('Name' => $data['Name']));
if ($query->num_rows() == 0) 
{
  $this->db->insert($this->main_table, $data);
  return $this->db->insert_id();
}
else 
{
  return false;
}

在插入数据之前检查重复数据。如果发生任何查询,请告诉我。