0

In codeigniter I want to delete record from a table. But at the same time I want to remove a record from another table which having foreign key to that record. Is there any possible way to do this using codeigniter?

1st delete query

$this->db->where('id',$id);
$this->db->delete('category_info');

1st table

id | description | image

2nd table

id | category_id | parent_category

2
  • You don't need PHP/codeigniter for this, just use mysql triggers after deletion. Commented Jul 14, 2014 at 10:59
  • But the framework I'm using is codeigniter Commented Jul 14, 2014 at 11:02

1 Answer 1

2

Instead of having PHP doing the heavy work, you can do this straight from MYSQL with a trigger. This will trigger after a deletion.

CREATE TRIGGER delete_trigger_tbl_2 AFTER DELETE ON category_info
FOR EACH ROW
BEGIN
DELETE FROM tbl_2
    WHERE tbl_2.id = old.id; 
-- Or category_id, not clear in your question.
END
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.