public function save($data, $id)
{
$this->db->insert OR REPLACE($this->table, $data);
return $this->db->insert_id();
}
如果数据不存在则插入,否则数据存在则替换
最佳答案
试试这个
block 引用>Check Unique data filed to check where record exist
public function save($data, $id) { $query = $this->db->query("SELECT * FROM table_name WHERE id = '{$data['id']}' "); $result = $query->result_array(); $count = count($result); if (empty($count)) { $this->db->insert('mytable', $data); } elseif ($count == 1) { $this->db->where('id', $data['id']); $this->db->update('mytable', $data); } }
关于php - 如何在 codeigniter 中使用插入或替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34241942/