provide your model code also what you are using in count_all function ?
Simply what you can do you check if the value exist in database something like this.
$query = $this->db->get_where('table_name',array('ad_code'=>$code,'ad_image'=>$image_link));
Check if $query->num_rows() > 0 ,if yes update else insert.
Feel free to let me know if you have any difficulty understanding it.
Regards,
Zeeshan.