Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Hi Gang. How about some mySQL error help!

I hope everyone's having a great Tuesday.

I am creating a database which enforces referential integrity (i.e. foreign key constraints). The front-end admin section allows the user to add, edit and delete 'people' from the database. 'people' can be assigned to 'roles' (like "teacher") and I'm ensuring a 'role' can't be deleted if it currently belongs to a 'person' ('person' being one row from 'people' table).

So this is all working fine - yay, referential integrity!

What I would like to change is what happens when the user tries to delete a 'role' that is assigned to a 'person'.

At the moment I'm using the PDO class from Randy's shirts4mike tutorial - a try catch block where the catch echos an error message 'data could not be updated' and exits the code.

The catch block kicks in for any database error, including when the user tries to delete a 'role' that is assigned to a 'person'. But what I would like to happen is a specific error message to be returned which says "you can't remove this role because it belongs to person x, y and z, without exiting the code.

I've no problem with creating and displaying the error message itself (i.e. finding the names of people x, y and z)...

... But I don't know how to tell phpMyAdmin, or perhaps the try-catch block to say "If this is a referential integrity error, say this error message instead".

To summarize: I need a try-catch block (or any other method?) with multiple error possibilities. These possibilities are triggered by retrieving the actual error type/message from phpMyAdmin.

Does anyone know where I should start?

Thanks!

Tom

4 Answers

try catch blocks can have more than one catch block. however, only one exception may be caught before it terminates. you pick the exception priority and then write a try{ //code }catch(){ //important error }catch(){ //less important error }finally{ //if needed this will always be executed. } find the exact error that is being thrown that you want to catch. catch it, then handle it. look through some exception resources to find what php calls a 'referential integrity' error.

To start research, I suggest you two articles:

PHP Manual - Exceptions

Catching multiple exception types in one catch block

That is fantastic, thank you to both of you! I don't know what I was googling before.. But it definitely wasn't the right phrase!

So my plan is to instead of exit the script, call a function which will set a (error) messages array in the $_SESSION. On the page, a function will run to see if any messages exist in the session, and if there are messages, they will be displayed until dismissed/refresh.

I've never done this before - do you think this is a good way? I mean.. it should work, but there might be a better way! Each catch/general validation will call my message function and be passed in the phrase to display and the type i.e. error, success, info.

If you have any advice, that would be fantastic! But no worries if not :-)

Be careful with Exception handling. you want to use it to handle errors, never use it to control the flow of a program. Input validation is always the best option, take input in, test it for validity via a method that has exception handling, show errors on page if they exist. never use a try{} catch(){} block as an if(){} else{} statement. Best way to do this, is to separate php files by function. Have a php file called validation.php with a class of functions, or just a list of functions, similar to a utility class, and include it in the main.php/index.php. Assuming you are using forms, when the form POSTs, take the $_POST['variable1', 'variable2'] then send it to your validation class/function list. It can perform a variety of tests on it and then if any errors exist it'll redirect back to the forms and list errors. This can be looped until valid input is put in. check these examples: http://code.tutsplus.com/tutorials/sanitize-and-validate-data-with-php-filters--net-2595 and http://myphpform.com/validating-forms.php keep learning and write lots of examples for yourself. good luck.

Awesome! Thanks.

That is the process I'm currently running - lot's of validation before the database connection. The additional catch block I'm looking for is to error if the user enters an attribute which matches another in a unique field. I guess this kind of validation would be better before submitting an SQL request - by reading all unique attributes in the table and comparing against the user input. I think I'll add this to the validations file :-)

Thanks for the links!