I need a help for avoid duplication entries on my database table using CodeIgniter.
I have a table called “user\_course\_tbl” with 3 columns as mentioned below.

1. Id
2. staff\_name
3. course\_code

I need to enter staff name and course code without duplications.

[![enter image description here](https://i.stack.imgur.com/2UNlx.png)](https://i.stack.imgur.com/2UNlx.png)

As mentioned in table, same person can add more different course codes. But same person couldn’t add same course code more than 1.

Ex: Smith has more different courses for the table. It will be ok. But Ann has same course more than 1. This should be avoiding. should not have same value for staff\_name and course\_code columns both together.

I wrote coding for the above matter. But I couldn’t understand to validate this matter.

**View**

```
<?php echo form_open_multipart('Course/insert_courses_for_users');?>
                <div class="box-body">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>User</label>
                            <select id="user" class="form-control" name="user" required>
                            <option value="">Select</option>
                            <?php
                            if(isset($user))
                            {
                                foreach($user as $cnt)
                                {
                                print "<option value='".$cnt['staff_name']."'>".$cnt['staff_name']."</option>";
                                }
                            } 
                            ?>
                            </select>
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Course Code</label>
                            <select id="CourseCode" class="form-control" name="courscode" onchange="getcoursename(this.value)" required>
                            <option value="">Select</option>

                            <?php

                            if(isset($coursecode))
                            {
                                foreach($coursecode as $cnt)
                                {
                                print "<option value='".$cnt['course_code']."'>".$cnt['course_code']."</option>";
                                }
                            } 

                        ?>

                            </select>
                        </div>
                    </div>

```

**Controller**

```
function insert_courses_for_users()
    {  
        $this->form_validation->set_rules('user', 'User', 'required');
        $this->form_validation->set_rules('courscode', 'Course Code', 'required');

        $id=$this->input->post('txtid');
        $user=$this->input->post('user');
        $coursecode=$this->input->post('courscode');

        $data=$this->Course_model->insert_courses_for_user(array('id'=>$id,'staff_name'=>$user,'course_code'=>$coursecode));

        if($data==true)
        {
            $this->session->set_flashdata('success', "New Course added for user Succesfully"); 
        }else
        {
            $this->session->set_flashdata('error', "Sorry, New Course added for user is Failed.");
        }

        redirect($_SERVER['HTTP_REFERER']);

    }

```

**Model**

```
function insert_courses_for_user($data)
    {
        $this->db->insert("user_course_tbl",$data);
        return $this->db->insert_id();

    }

```

Could you please help to solve this matter?