php - 在 Codeigniter 中将重复值插入数据库

标签 php mysql codeigniter

我正在将一些数据插入到我的 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);
        }
    }

型号

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

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

最佳答案

我也遇到了同样的问题,

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

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

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

关于php - 在 Codeigniter 中将重复值插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21163288/

相关文章:

php - 在新页面上提交表单php中有另一个新表单

php - 使用 foreach 循环在 codeigniter 中添加 OR WHERE 语句

codeigniter - Codeigniter ionic 授权 : Where are the email templates?

php - Laravel 5.2 错误 App\User cannot use Illuminate\Foundation\Auth\User - 这不是特征

php - 使用 PHP 为每个 img 添加父级

mysql - 我可以拥有 HA MySQL/MariaDB Slave 吗?

php - 拉维尔 5.3 : What is causing 'maximum execution time of 30 seconds exceeded'

css - 代码点火器 : base_url() at CSS file doesn't work

php - 数据库连接设置放在哪里?

php - 如何计算与此查询中的字符串文本匹配的数据数量?