title: Displaying appropriate error messages for users
body: <p><strong>UPDATE 1:</strong></p>
  
  <p>I now have the following code:</p>
  
  <pre><code>} catch( PDOException $e ) {
      error_log( $e -&gt; getMessage() );
  
      switch( $e -&gt; getCode() ) {
          case 1452:
              echo "Sorry, the referral ID you have entered does not exist.";
              break;
          default:
              echo $e -&gt; getMessage();      
      }
  }
  </code></pre>
  
  <p>But it keeps giving me the following error:</p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint
    violation: 1452 Cannot add or update a
    child row: a foreign key constraint
    fails (database1.table2, CONSTRAINT
    fk_referals_users1 FOREIGN KEY
    (users_id) REFERENCES users (id) ON
    DELETE NO ACTION ON UPDATE NO ACTION)</p>
  </blockquote>
  
  <p>Should it not be giving me</p>
  
  <blockquote>
    <p>Sorry, the referral ID you have
    entered does not exist.</p>
  </blockquote>
  
  <p>as it's going to default: which is showing the full 1452 error.  As it's a 1452 error, should't it be going to the case 1452?</p>
  
  <p><strong>ORIGINAL QUESTION:</strong></p>
  
  <p>I have the following script which is setup for development purposes.  When this will go live, I would like to display more appropriate error messages.</p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint
    violation: 1452 Cannot add or update a
    child row: a foreign key constraint
    fails (<code>database1</code>.<code>table2</code>,
    CONSTRAINT <code>fk_referals_users1</code>
    FOREIGN KEY (<code>users_id</code>) REFERENCES
    <code>users</code> (<code>id</code>) ON DELETE NO ACTION ON
    UPDATE NO ACTION)</p>
  </blockquote>
  
  <p>This happens if the referral value they enter does not exist in table1, so it cannot be inserted into table2 because of the constraints.</p>
  
  <p>This is the script I currently have.  How do I capture specific messages like the one above and more, and display them something like:</p>
  
  <blockquote>
    <p>Sorry, the referral ID you have
    entered does not exist.</p>
  </blockquote>
  
  <p>This is the section I am trying to edit in terms of friendly error messages:</p>
  
  <pre><code>try {
      $DBH = new PDO( "mysql:host=localhost;dbname=database1", "user", "pass" );
      $DBH -&gt; setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  
      $STH = $DBH -&gt; prepare( "insert into database1.table1 (display_name, email, password) values ( :display_name, :email, :password )" );
  
      $STH -&gt; bindParam( ':display_name', $_POST['display_name'], PDO::PARAM_STR, 100 );
      $STH -&gt; bindParam( ':email', $_POST['email'], PDO::PARAM_STR, 100 );
      $STH -&gt; bindParam( ':password', $_POST['password'], PDO::PARAM_STR, 100 );
  
      $STH -&gt; execute();
  
      try {
          $STH = $DBH -&gt; prepare( "insert into database1.table ( username, status, users_id ) values ( :username, :status, :users_id )" );
  
          $strStatus = 1;
  
          $STH -&gt; bindParam( ':username', $_POST['display_name'], PDO::PARAM_STR, 100 );
          $STH -&gt; bindParam( ':status', $strStatus, PDO::PARAM_INT, 1 );
          $STH -&gt; bindParam( ':users_id', $_POST['referer'], PDO::PARAM_INT, 1 );
  
          $STH -&gt; execute();
      }
  
      $DBH = null;
  
      header( "Location: ".$_SERVER['PHP_SELF'] );
      exit;
  } catch( PDOException $e ) {
      echo $e -&gt; getMessage();
  }
  </code></pre>