【问题标题】:Duplicate values insert into the database in Codeigniter在 Codeigniter 中将重复值插入数据库
【发布时间】:2014-01-16 13:35:00
【问题描述】:

我在我的 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;
}

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

【问题讨论】:

  • 在你的模型中你写了return $data;,但在你的控制器中它在哪里捕获$data,因为$this->favourite_db->add_soothingmemory($data);没有分配给任何变量?
  • 你建议我做什么?
  • `$query = $this->favourite_db->getsoothingcount() + 1;` 有什么作用?
  • getsoothingcount() 用于更新数据库中的用户访问次数
  • $this->favourite_db->updateusercount($count); 增加了多少分贝值?

标签: php mysql codeigniter


【解决方案1】:

我遇到了同样的问题,

无论如何,在你的模型中,

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

这样可以避免重复。 希望对你有帮助

【讨论】:

    【解决方案2】:

    进行以下更改:

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

    【讨论】:

    • 我改变了它,但它做同样的事情
    • 尝试从控制器中移除 getsoothingcount() 模型,看看它是否可以工作。还在 config.php 中自动加载数据库
    猜你喜欢
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 2014-05-04
    相关资源
    最近更新 更多