如果出现错误,是否可以更改我的搜索脚本以将客户端定向到其他页面?
我没有找到“没有找到结果!”的文字,而是希望将它们定向到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; }
?>
答案 0 :(得分: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;
}
}
?>
答案 1 :(得分:0)
使用标头重定向代替echo
错误。实施例。
header("Location: search_error1.php");