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...
try/catchfor this because$this->db->querydoesn't throw anExceptionwhen it fails. You should fix your SQL query.SELECTfirst to check if the row exists. If it doesn't, then don't run theDELETEand return your own error.on delete restrictFK in product.purchase_id. so if user try to delete this include key, database must show an error.........SELECTquery first to make sure the row/key you want exists.