Mysql
 sql >> Database >  >> RDS >> Mysql

Aggiorna una riga, ma inseriscila se la riga non esiste in codeigniter

Prima seleziona tutto image_id dalla tabella.

$data = $this->db->select(`image_id`)->get($this->table_name)->result_array();

Elenca image_id in un array.

$image_ids=array();

foreach($data as $key => $value):

$image_ids[$key]=$value[`image_id`];

endforeach;

$update = array(
   array('image_id' => 1, 'name' => 'Party Gustav'),
   array('image_id' => 2, 'name' => 'Peter'),
   array('image_id' => 3, 'name' => 'Lisa')
)

Verifica se gli image_id esistono:

$update_query= $this->db->where_in(`image_ids`,$image_ids)
               ->get($this->table_name)->result();

if($update_query->num_rows() > 0):

  $this->db->update_batch($update,$this->table_name);//update if ids exist
else
   $this->db->insert_batch($update,$this->table_name);//insert if does not exist
endif;