Displaying appropriate error messages for users
UPDATE 1:
I now have the following code:
} catch( PDOException $e ) {
error_log( $e -> getMessage() );
switch( $e -> getCode() ) {
case 1452:
echo "Sorry, the referral ID you have entered does not exist.";
break;
default:
echo $e -> getMessage();
}
}
But it keeps giving me the following error:
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)
Should it not be giving me
Sorry, the referral ID you have entered does not exist.
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?
ORIGINAL QUESTION:
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.
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
database1
.table2
, CONSTRAINTfk_referals_users1
FOREIGN KEY (users_id
) REFERENCESusers
(id
) ON DELETE NO ACTION ON UPDATE NO ACTION)
This happens if the referral value they enter does not exist in table1, so it cannot be inserted into table2 because of the constraints.
This is the script I currently have. How do I capture specific messages like the one above and more, and display them something like:
Sorry, the referral ID you have entered does not exist.
This is the section I am trying to edit in terms of friendly error messages:
try {
$DBH = new PDO( "mysql:host=localhost;dbname=database1", "user", "pass" );
$DBH -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$STH = $DBH -> prepare( "insert into database1.table1 (display_name, email, password) values ( :display_name, :email, :password )" );
$STH -> bindParam( ':display_name', $_POST['display_name'], PDO::PARAM_STR, 100 );
$STH -> bindParam( ':email', $_POST['email'], PDO::PARAM_STR, 100 );
$STH -> bindParam( ':password', $_POST['password'], PDO::PARAM_STR, 100 );
$STH -> execute();
try {
$STH = $DBH -> prepare( "insert into database1.table ( username, status, users_id ) values ( :username, :status, :users_id )" );
$strStatus = 1;
$STH -> bindParam( ':username', $_POST['display_name'], PDO::PARAM_STR, 100 );
$STH -> bindParam( ':status', $strStatus, PDO::PARAM_INT, 1 );
$STH -> bindParam( ':users_id', $_POST['referer'], PDO::PARAM_INT, 1 );
$STH -> execute();
}
$DBH = null;
header( "Location: ".$_SERVER['PHP_SELF'] );
exit;
} catch( PDOException $e ) {
echo $e -开发者_Go百科> getMessage();
}
The full exception message is being output here:
catch( PDOException $e ) {
echo $e -> getMessage();
}
You can log the full exception here, and echo any message you like
catch( PDOException $e ) {
error_log($e -> getMessage());
echo "A error has occurred";
}
If you wanted to handle different error messaged differenty, you could switch on the error code
catch( PDOException $e ) {
error_log($e -> getMessage());
switch($e->getCode()){
case 23000:
echo "Sorry, the referral ID you have entered does not exist."
break;
default:
echo "A error has occurred";
}
}
精彩评论