I have two tables Product and Purchase.
I set a on delete restrict FK in product.purchase_id column with purchase.purchase_id column. Then if I try to delete product.product_id with FK, It shows error just like
A Database Error Occurred Error Number: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`another_bata`.`product_purchase_item`, CONSTRAINT `FK_product_purchase_item_1` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`) ON UPDATE CASCADE) delete from product where product_id='158' Filename: C:\xampp\htdocs\rima_shoe\system\database\DB_driver.php Line Number: 330
But I want only alert for this. so I just try a try...catch syntax my code is here
try{
$this->db->query("delete from product where product_id='$delete'");
}
catch (Exception $e) {
echo "an error occured during query" ;
}
But this code doesn't work. it also display above error in a white page...
Reference Codeigniter foreign key constraint check
in the database.php file find the line
$db['default']['db_debug']
and set it to FALSE. This will prevent Codeigniter from printing the error on the screen.
Also in the delete function you can check:
if ($this->db->_error_number() == 1451)
If you need to do something special when this happens.
Edit Remeber that in Codeigniter 3.0 (CI3), this is now $errors = $this->db->error();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With