在PHP中提交标题位置后表单不重定向不起作用

时间:2017-08-10 00:20:02

标签: php redirect header location

我的PHP标题重定向不起作用,我似乎无法找出原因。我已经阅读了很多问题,而且我尝试过的任何事情似乎都没有用。

<?php
define('DBHOST','shareddb1d.hosting.stackcp.net');
define('DBUSER','JoplinLeftHand-3231135a');
define('DBPASS','Banoodle1!');
define('DBNAME','JoplinLeftHand-3231135a');

$link = mysql_connect(DBHOST, DBUSER, DBPASS);

if (!link) {
    die('Could Not Connect: ' . mysql_error());
} else {
    echo "You Are Connected<br>";
}

$db_selected = mysql_select_db(DBNAME, $link);

if (!$db_selected) {
    die('Can\'t Use ' . DBNAME . ': ' . mysql_error());
} else {
    echo "Database Selected";
}
$tid = $_POST['tid'];
$first = $_POST['first'];
$last = $_POST['last'];
$zip = $_POST['zip'];
$descrip = $_POST['descrip'];

$sql = "INSERT INTO tracking (tracking_id, tracking_first, tracking_last, tracking_zip, tracking_descrip) VALUES ('$tid', '$first', '$last', '$zip', '$descrip')";

if (!mysql_query($sql)) {
    die('Error: ' . mysql_error());
} else {
    header("Location:/?p5");
}

1 个答案:

答案 0 :(得分:1)

header('Location: ...');必须是脚本的第一个输出,否则它将无效。在执行重定向之前,您使用了echo两次:

echo "You Are Connected<br>";

在这里:

echo "Database Selected";

此外,某些客户端要求Location标头中传递的URL为绝对URL,而不是相对URL,例如:

header("Location: http://example.com/?p5");