重复值插入Codeigniter中的数据库

时间:2014-01-16 13:35:00

标签: php mysql codeigniter

我在Codeigniter控制器中将一些数据插入数据库

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

模型

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

问题是当值插入数据库时​​,它会插入三次以创建多个/重复的行。

2 个答案:

答案 0 :(得分:0)

进行以下更改:

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

在模型中

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

答案 1 :(得分:0)

我遇到了同样的问题,

无论如何,在您的模型中

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

那样可以避免重复。 希望对您有帮助