Mavisa9
Mavisa9

Reputation: 140

Prevent Duplicate Entry php with Codeigniter

I am trying to prevent duplicate entries with codeigniter, but it doesn't work anymore.

The data still insert to database despite the same name.

This is the controller code

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

this is the Model..

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

and this is the view

<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>

Upvotes: 0

Views: 4828

Answers (3)

Mavisa9
Mavisa9

Reputation: 140

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

Upvotes: 1

Jaymin
Jaymin

Reputation: 1661

Try doing this inside save function

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

My Model

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

Checking duplication data before inserting the data. Let me know if any query occurs.

Upvotes: 0

dougtesting.net
dougtesting.net

Reputation: 571

Where does $Name on this line come from?

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

Also it should not have quotes around it, should be...

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

Upvotes: 0

Related Questions