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
I'm using this approach:
configure your table mytable
with unique id
and unique
key for the column xyz
, which you want to update
try to insert an array $data
, using INSERT IGNORE INTO
do avoid duplicates
$insert_query = $this->db->insert_string('bt_ical_list', $data);
$insert_query = str_replace('INSERT INTO','INSERT IGNORE INTO',$insert_query);
$this->db->query($insert_query);
this inserts a row, if value in column xyz
doesn't exist
use function insert_id()
to return an id
if row was inserted, update if no row was inserted.
if(!$this->db->insert_id()){
$query=$this->db ->where('xyz', $data['xyz'])
->update('my_table',$data);
};