Omade
Omade

Reputation: 47

Duplicate values insert into the database in Codeigniter

I am inserting some data into the database in my Codeigniter controller

 function addsoothingmemory() {
        $config['upload_path'] = './uploads2/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '500000';
        $config['max_width'] = '100024';
        $config['max_height'] = '100768';

        $this->upload->initialize($config);

        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('soothing', $error);
        } else {

            $imagedata = $this->upload->data();

            $data = array(
                'image_path' => $imagedata['file_name'],
                'title' => $this->input->post('title'),
                'user_id' => $this->session->userdata("user_id"),
                'feelingrate' => $this->session->userdata("feelingrate"),
                'description' => $this->input->post('description')
            );

            $query = $this->favourite_db->getsoothingcount() + 1;
            $this->favourite_db->add_soothingmemory($data);
            $count = array('soothingcount' => $query);
            $this->favourite_db->updateusercount($count);
            $this->load->view('positivepast', $data);
        }
    }

Model

function add_soothingmemory($data) {
    $this->load->database();
    $this->db->insert('soothingmemory', $data);
    $this->set_session();
    return $data;
}

The problem is when the value is inserted into the database it inserts three times creating multiple/duplicate rows.

Upvotes: 0

Views: 2315

Answers (2)

Numan
Numan

Reputation: 127

I had the same problem,

anyhow, in your model,

$this->db->limit(1);
$this->db->insert('soothingmemory', $data);

that way duplicates will be avoided. Hope it helps

Upvotes: 2

Hubert
Hubert

Reputation: 169

Make the following changes:

function addsoothingmemory() {
        $config['upload_path'] = './uploads2/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '500000';
        $config['max_width'] = '100024';
        $config['max_height'] = '100768';

        $this->upload->initialize($config);

        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('soothing', $error);
        } else {

            $imagedata = $this->upload->data();

            $data = array(
                'image_path' => $imagedata['file_name'],
                'title' => $this->input->post('title'),
                'user_id' => $this->session->userdata("user_id"),
                'feelingrate' => $this->session->userdata("feelingrate"),
                'description' => $this->input->post('description')
            );

            $query = $this->favourite_db->getsoothingcount() + 1;
            $save = $this->favourite_db->add_soothingmemory($data);
           //  Move this code into the model 
           //  $count = array('soothingcount' => $query);
            if($save == true){
            $this->favourite_db->updateusercount($query);
            $this->load->view('positivepast', $data);
            }else{
            //do something else
            }
        }
    }

In the model

function add_soothingmemory($data) {
    $this->load->database();
    $save = $this->db->insert('soothingmemory', $data);
    //This condition will check, if the $data was save
    if($save == true){
    //If is save, it will set the session and return true
    $this->set_session();
    return true;
    }else{
     return false;
    }
}

Upvotes: 0

Related Questions