CodeIgniter- active record insert if new or update on duplicate

前端 未结 8 618
醉酒成梦
醉酒成梦 2020-12-29 04:46

Is it possible to do an active record query in CodeIgniter that will update an existing record if one already exists or insert if it doesnt, for the given k

8条回答
  •  礼貌的吻别
    2020-12-29 05:29

    Updated Milaza's answer - Simply done

        $updt_str = '';
        foreach ($array as $k => $v) {
            $updt_str = $updt_str.' '.$k.' = '.$v.',';
        }
        $updt_str = substr_replace($updt_str,";", -1);
        $this->db->query($this->db->insert_string('table_name', $array).' ON DUPLICATE KEY UPDATE '.$updt_str);
    

提交回复
热议问题