0

如果出现错误,是否可以更改我的搜索脚本以将客户端引导到不同的页面?

我希望将它们定向到 search_error1.php,而不是出现文本“未找到结果!”。

我希望将它们定向到 search_error2.php,而不是出现“您尚未输入搜索字段”文本。

<?php

$error = '';

if (isset($_POST['Submit'])) {
if (!empty($_POST['reg'])) {

$record = $_POST['reg'];

$query = mysql_query("SELECT * FROM reg_add WHERE reg='" . mysql_real_escape_string($record) . "'");
$result = mysql_num_rows($query);

if ($result != 0) {

$row = mysql_fetch_array($query);

$first_name = $row['first_name'];
$last_name = $row['last_name'];
$reg = $row['reg'];

} else {$error = 'No result have been found!';}

} else {$error = 'You have not entered the search field, <a     href="javascript:history.back(1)">Go back</a>.';}

}

if (!empty($error)) { echo $error; } 

?>
4

2 回答 2

2

是的

采用header()

http://se2.php.net/manual/en/function.header.php

} else { header("Location: search_error1.php"); exit; }

} else { header("Location: search_error2.php"); exit; }

您的代码(+ 预期)如下所示:

<?php
if (isset($_POST['Submit'])) {
    if (!empty($_POST['reg'])) {

        $record = $_POST['reg'];

        $query = mysql_query("SELECT * FROM reg_add WHERE reg='" . mysql_real_escape_string($record) . "'");
        $result = mysql_num_rows($query);

        if ($result != 0) {

            $row = mysql_fetch_array($query);

            $first_name = $row['first_name'];
            $last_name = $row['last_name'];
            $reg = $row['reg'];

        } else {
            header("Location: search_error1.php");
            exit;
        }

    } else {
        header("Location: search_error2.php");
        exit;
    }
}
?>
于 2012-04-26T16:36:41.537 回答
0

而不是echoing 错误,使用标头重定向。例子。

header("Location: search_error1.php");

于 2012-04-26T16:36:52.787 回答