0

致命的なエラー: キャッチされない例外 'Exception' とメッセージ '

エラー: 次のフィールドが入力されていません - 9 行目の /vagrant/web/Assignment4/Person.php の Last Name '

すべてのフィールドが入力されていることを確認するためにフォームをチェックしようとしています。それらのいずれかが空の場合、どのフィールドが空であるかを示すエラーをスローしたいと考えています。例外のキャッチがどのように機能するかを理解するのに苦労しているので、これを修正する方法を誰か教えてもらえますか?

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>");
               // }





   }
4

1 に答える 1

1

ユーザーにエラーを表示したい場合、例外をスローするのは間違った方法です。これを試してください:

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

また、お楽しみに、必要な HTML5 プロパティを確認してください。

<input type="text" required="required" value="" />
于 2012-10-27T18:15:41.997 に答える