CodeIgniter Forums

Full Version: How to add data having foreign key
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have to insert data into my data base it contains foreign key and I am unable to find any guide Sad which tells how insert thin in codeigniter. Can any one help me out Smile
Please rephrase. Your question is not clear.
If you mean that you need the last insert key of a new data to map it as foreign key, than you can get it like that:

PHP Code:
$this->db->insert('table',$data);
$fk_id $this->db->insert_id(); 
You may be inserting a foreign key ID in a 'child' table that doesn't yet exist in the 'parent' table. Make sure that the foreign key actually exists in the 'parent' table before referencing it in the 'child'.