siezma Posted October 31, 2006 Share Posted October 31, 2006 Hello,I have a question about foreign key issues.When i try to delete one record in the database(which has some related record in another table - foreign key).The script shows an error message(coming from mysql_error()).The content of the message is below;"Cannot delete or update a parent row: a foreign key constraint fails "What i want to do is to analyze the error and show more user friendly messages such as"you can't delete this record,because there are some related records etc.. "How can i do that with php? I wonder how other programmers handle this issue with php?Thanks Quote Link to comment Share on other sites More sharing options...
bqallover Posted October 31, 2006 Share Posted October 31, 2006 You could do something like:[code]$err_num = mysql_errno();if( $err_num == 1217 ) // the number of the error you specified{ echo "you can't delete this record, because there are some related records etc..";}[/code]You can find a list of error codes at [url=http://dev.mysql.com/doc/refman/5.0/en/error-handling.html]http://dev.mysql.com/doc/refman/5.0/en/error-handling.html[/url]Also, if the query automatically echos errors (can't remember off the top of my head) you could prefix it with an @.Another technique might be to create an array of strings of the form$friendlySqlErr[1217] = "you can't delete this record, because there are some related records etc..";possibly as a reusable library? Then have something like[code]$err_num = mysql_errno();if( isset( $friendlySqlErr[$err_num] ){ echo $friendlySqlErr[$err_num];}else{ echo mysql_error();}[/code] Quote Link to comment Share on other sites More sharing options...
siezma Posted October 31, 2006 Author Share Posted October 31, 2006 Thank you very much bqallover!. This is exactly what i need.Bye Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.