为用户显示适当的错误消息

发布于 2024-10-26 23:17:46 字数 2448 浏览 1 评论 0原文

更新1:

我现在有以下代码:

} 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();      
    }
}

但它一直给我以下错误:

SQLSTATE[23000]:完整性约束 违规:1452 无法添加或更新 子行:外键约束 失败(database1.table2,CONSTRAINT fk_referals_users1 外键 (users_id) 引用用户 (id) ON 删除无操作更新无操作)

应该不会给我

抱歉,您拥有的推荐 ID 输入的内容不存在。

因为它将默认:显示完整的 1452 错误。既然是1452错误,那不是应该去case 1452吗?

原始问题:

我有以下脚本,该脚本是出于开发目的而设置的。当它上线时,我想显示更合适的错误消息。

SQLSTATE[23000]:完整性约束 违规:1452 无法添加或更新 子行:外键约束 失败(数据库1表2, 约束fk_referals_users1 外键 (users_id) 参考 users (id) ON 删除 无操作 更新无操作)

如果他们输入的引用值在 table1 中不存在,则会发生这种情况,因此由于约束而无法将其插入到 table2 中。

这是我目前拥有的脚本。如何捕获像上面这样的特定消息以及更多消息,并显示它们,例如:

抱歉,您拥有的推荐 ID 输入的内容不存在。

这是我试图根据友好错误消息进行编辑的部分:

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 -> getMessage();
}

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,
CONSTRAINT fk_referals_users1
FOREIGN KEY (users_id) REFERENCES
users (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 -> getMessage();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

·深蓝 2024-11-02 23:17:46

完整的异常消息正在此处输出:

catch( PDOException $e ) {
    echo $e -> getMessage();
}

您可以在此处记录完整的异常,并回显您喜欢的任何消息

catch( PDOException $e ) {
    error_log($e -> getMessage());
    echo "A error has occurred";
}

如果您想处理不同的错误消息,您可以打开错误代码

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

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";        
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文