3

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...

7
  • 1
    What's your question? You can't use try/catch for this because $this->db->query doesn't throw an Exception when it fails. You should fix your SQL query. Commented Oct 30, 2013 at 19:51
  • so how can I solve my problem... A user don't like this database error page if user try a FK id. I just want ignore this page, I want a session flash data or $data['test'] to trans this error message Commented Oct 30, 2013 at 20:01
  • It's better to fix an SQL error versus just ignoring it. Try to run a SELECT first to check if the row exists. If it doesn't, then don't run the DELETE and return your own error. Commented Oct 30, 2013 at 20:02
  • how can I fix sql error? I set a on delete restrict FK in product.purchase_id. so if user try to delete this include key, database must show an error......... Commented Oct 30, 2013 at 20:05
  • Try to run a SELECT query first to make sure the row/key you want exists. Commented Oct 30, 2013 at 20:06

2 Answers 2

4

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();

Sign up to request clarification or add additional context in comments.

Comments

3

The codeigniter query does not throw and exception. Instead use this to figure out if an error occured.

if ($this->db->_error_message()){
    $msg = $this->db->_error_message();

    echo $msg;
}

Simple as that

2 Comments

It doesn't throw an exception, but it still triggers a warning (which displays an error page).
@SajunaFernando sir i use your code but it not echo errror message

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.