Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter update() defaults to insert()

Is this possible / true?

For instance, if I want to update a row with the id of 1, but no row exists, will this function (update function) automatically insert a new row with the given data? Is there a parameter to enable this?

If not, what would be the best way to handle this (if possible) without using a separate function to evaluate the table and return bool if the row exists. (Then UPDATEing or INSERTing from there)

Not much of an issue.. Just looking for clarification and wondering if anyone knows about this.

like image 609
Patrick Avatar asked Dec 09 '22 16:12

Patrick


1 Answers

I found a good solution in this link, I hope it helps if anyone is still looking for a solution in codeigniter:

$sql = 'INSERT INTO menu_sub (id, name, desc, misc)
        VALUES (?, ?, ?, ?)
        ON DUPLICATE KEY UPDATE 
            name=VALUES(name), 
            desc=VALUES(desc), 
            misc=VALUES(misc)';

$query = $this->db->query($sql, array( $id, 
                                       $this->validation->name, 
                                       $this->validation->desc, 
                                       $this->validation->misc
                                      ));  
like image 54
Zack Avatar answered Jan 04 '23 01:01

Zack