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