0

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.

8
  • in your model you wrote return $data; but in your controller where is it catching $data, as $this->favourite_db->add_soothingmemory($data); is not assigned to any variable? Commented Jan 16, 2014 at 13:37
  • what do you suggest I do please? Commented Jan 16, 2014 at 13:39
  • 1
    What does ` $query = $this->favourite_db->getsoothingcount() + 1;` do? Commented Jan 16, 2014 at 13:47
  • the getsoothingcount() is used to update the number of user access in the database Commented Jan 16, 2014 at 13:50
  • how much does this $this->favourite_db->updateusercount($count); increment the db value? Commented Jan 16, 2014 at 14:31

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

I changed it but it does the same thing
Try removing the getsoothingcount() model from the controller and see if it will work. Also autoload the database in the config.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.