CodeIgniter Forums
insert data into 2 table - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: insert data into 2 table (/showthread.php?tid=66979)



insert data into 2 table - zembarekosaputra - 12-27-2016

i wanna ask you for all of the expertttt in CI, i've got a problem, i have a form that contain "id_diskon". I want to insert that "id_diskon" attribute into two different table,  which is table diskon and table diskon tunai. in table diskon, "id_diskon" is a primary key and in table diskon tunai, "id diskon"is a primary key foreign key, so this is an "is a" case in erd diagram. my question is, can you guys tell me the query for the model and controller to run that form, because when i run in my text editor, i've always got an error message like this picture above.


RE: insert data into 2 table - suhindra - 12-27-2016

In order to insert the primary key of table diskon into table diskon_tunai, codeIgniter has built in helper functions to help with this task.
PHP Code:
//prepare data for table diskon    
$data = array(
  'content' => 'My content',
);
//insert into table diskon
$this->db->insert('diskon',$data);

//prepare data for table diskon_tunai
$data = array(
  'id_diskon' => $this->db->insert_id(),
);
//insert into table diskon_tunai
$this->db->insert('diskon_tunai',$data);