Tyler Tracy
Tyler Tracy

Reputation: 73

Form Not Redirecting After Submit Header Location in PHP Not Working

My PHP header redirection isn't working and I can't seem to figure out why. I've read through a lot of questions and nothing I've tried seems to work.

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

Upvotes: 0

Views: 525

Answers (1)

Enstage
Enstage

Reputation: 2126

header('Location: ...'); has to be the first output of the script, otherwise it will not work. You are using echo twice before performing the redirect, here:

echo "You Are Connected<br>";

and here:

echo "Database Selected";

Also, SOME clients require the URL passed in the Location header to be absolute URLs, and not relative, for example:

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

Upvotes: 1

Related Questions