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

Robert Walker
Robert Walker
17,146 Points

PDOException not throwing error in foreach loop

foreach($postedFormData['checkboxes'] as $id){

            try{

                $statement = $db_handle->prepare("DELETE FROM admin_users WHERE admin_id= :id");
                $statement->execute(array(":id" => $id));

            } catch (PDOException $e) {

                $this->logError($e->getMessage());
                $errorMessage = array("error" => "Failed to delete admin users!");

                print json_encode($errorMessage);   

                exit();
            }
        }

Feeling rather sheepish about this as I am sure it is something simple but I am failing hard.

This works perfect for deleting the correct admin users but when i changed some of the values to test error handling I was a little shocked that no error was thrown even though the admin_id didnt even exist.

For some reason the PDOException is not throwing while in this foreach loop / the catch is never being entered even when the admin row is not valid.

Any ideas?