Brian Houlihan
Brian Houlihan

Reputation: 71

Uncaught Exception Error PHP

Fatal error: Uncaught exception 'Exception' with message '

Error: The following fields have not been filled out- Last Name ' in /vagrant/web/Assignment4/Person.php on line 9

I am trying to check a form to make sure that all of the fields are filled in. If any of them are empty, I want to throw an error that says which fields are empty. I am having a hard time understanding how catching exceptions work, so could anyone tell me how they would fix this?

Person.php

 public function insert()
{

  //Storing required $_POST array fields in variable for isset function    

   $expectedValues = array(
       "firstName"   => "First Name",
       "lastName"    => "Last Name",
       "title"       => "Title",
       "office"      => "Office",
       "phoneNumber" => "Phone Number",
       "email"       => "Email",
       );


   //Checking to see if all fields are filled in 

    foreach ($expectedValues as $field => $humanName) { 
       if (empty($_POST[$field])) { 
        $errorArray[] = $humanName;
         foreach($errorArray as $print){
            throw new Exception("<p>" . "Error: The following fields have not been filled out- " . $print . "</p>");
         }

         try{
             (count($errorArray) = 0);
         }
         catch(Exception $e){
             echo "<p>" . $e->getMessage() . "</p>";
         }
       } 
     }


       //If they are, insert them into the Perosn table    

               $insert = $this-> doQuery("INSERT INTO Person 
                                         VALUES(
                                        '$_POST[firstName]',
                                        '$_POST[lastName]',
                                        '$_POST[title]',
                                        '$_POST[office]',
                                        '$_POST[phoneNumber]',
                                        '$_POST[email]')");

                                  $insert;


         //If insert query is successful, return true 
            if ($insert === true){
                return true; 
                echo "Congragulations! You now work for Assignment 4 Incorporated";
            }


         //If not, throw an exception    

            //else{
              //  throw new Exception
               // ("<p>" . "Error: Query was unsuccessful:" 
               // . " " . $this->error . "</p>");
               // }





   }

Upvotes: 0

Views: 336

Answers (1)

Alvar FinSoft Soome
Alvar FinSoft Soome

Reputation: 740

If you want to display errors to user then throwing exceptions is wrong way, try this:

foreach ($expectedValues as $field => $humanName) { 
   if (empty($_POST[$field])) { 
       $errorArray[] = $humanName;
   } 
 }
 if (count($errorArray) > 0) {
      echo 'Following fields are empty: '.implode(' ', $errorArray);  
 }

Also for the fun, check out HTML5 property required:

<input type="text" required="required" value="" />

Upvotes: 1

Related Questions