本文介绍了在 MySQL 中创建重复条目时重定向到自定义错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当出现重复条目​​时,我试图将用户重定向到自定义错误页面(例如:error.php),这是插入语句的示例,但我不知道如何进行重定向.

I am trying to redirect users to a custom error page (for example: error.php) when a duplicate entry is made, this is a sample of the insert statement, but I don't know how to do the redirecting.

$values = $_POST;
foreach ($values as &$value) {
    $value = mysql_real_escape_string($value);
}

$sql1="INSERT INTO loan (loan_id)
VALUES ('$values[loan_id]')";

$result = mysql_query($sql1);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

推荐答案

重定向表单输入的正确方法是使用 HTTP 303 重定向.

The proper way to redirect form input is using the HTTP 303 redirect.

要重定向到当前行,请在 PHP 中执行以下操作:

To redirect at the current line, do so something like this, in PHP:

// Tell the browser to redirect
header('Location: /error.php', TRUE, 303);

// This ensures that the script doesn't continue
// Also, it shows the error on the off chance their browser doesn't redirect
die('Input error.');

您必须确保在任何输出之前包含标头函数.

You must ensure that you include the header function before any output.

在您的代码中:

if (!$result) {
    header('Location: /error.php', TRUE, 303);
    die('Invalid query: ' . mysql_error());
} else {
    header('Location: /success.php', TRUE, 303);
    die('Success');
}

这篇关于在 MySQL 中创建重复条目时重定向到自定义错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:44