In order to avoid a user creating duplicate data I use a UNIQUE constraint on a field. Everything works perfect until a duplicate entry; which immediately gives me an internal server error. I am using CodeIgniter for queries and check database errors using $this->db->_error_message() yet instead of being handled the program seems to exit and the error number is returned on my AJAX call.
Is there another way of handling these types of errors or is it something I am doing wrong?
-
you might wanna post relevant code.user1978142– user19781422014-04-29 03:43:10 +00:00Commented Apr 29, 2014 at 3:43
-
Sounds like an Exception gets thrown, and you neglected to catch it properly.C3roe– C3roe2014-04-29 03:45:11 +00:00Commented Apr 29, 2014 at 3:45
-
@CBroe What was it that threw the exception and how do I handle it?boompow– boompow2014-04-29 03:48:03 +00:00Commented Apr 29, 2014 at 3:48
-
see if you are getting any error on apache error_log or whatever webserver you are usingk961– k9612014-04-29 04:02:23 +00:00Commented Apr 29, 2014 at 4:02
Add a comment
|
1 Answer
Inserting a duplicate will throw the exeption.
Try to catch it like this:
try
{
// insertin the duplicate
}
catch (Exception $e)
{
echo 'Exeption: '. $e->getMessage();
}
More about exception handling: http://www.php.net/manual/en/language.exceptions.php
2 Comments
boompow
That did not work. After the duplicate error is fired execution stops completely and the
try/catch block does not do anything.Aret
Maybe this will help: stackoverflow.com/questions/15858372/…