header() not redirecting for some reason...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

header() not redirecting for some reason...

Post by boo_lolly »

i'm writing a redirect.php page for a CMS. the admin user makes whatever changes they want to certain information in the database, then clicks submit. they are taken to redirect.php which handles all the sql commands depending on what the admin wanted to change. and then they are redirected to another page, depending on the types of changes they made, as well... the header is not working. here's the error:

Warning: Cannot modify header information - headers already sent by (output started at /../../../admin/redirect.php) in /../../../admin/redirect.php on line 41

Code: Select all

<?php
	$uID        = $_POST['editID'];
	$sub_opt    = $_POST['sub_opt'];
	$brideFname = $_POST['brideFname'];
	$brideLname = $_POST['brideLname'];
	$groomFname = $_POST['groomFname'];
	$groomLname = $_POST['groomLname'];
	$ship_add   = $_POST['ship_add'];
	$ship_city  = $_POST['ship_city'];
	$ship_state = $_POST['ship_state'];
	$ship_zip   = $_POST['ship_zip'];
	$_POST['event_month'];
	$_POST['event_day'];
	$_POST['event_year'];
	
	if($_POST['event_month2'] != NULL){
		$event_month = $_POST['event_month2'];
	}if($_POST['event_day2'] != NULL){
		$event_day = $_POST['event_day2'];
	}if($_POST['event_year2'] != NULL){
		$event_year = $_POST['event_year2'];
	}

	if($sub_opt == "cancel"){
		echo "<CENTER>All changes were cancelled. You are being redirected to Home.</CENTER>";
		$redirect = "admin1.php?action=view_all";
	}elseif($sub_opt == "save"){
		//open connection
		//SQL commands
		//close connection
		echo "<CENTER>Your changes have been saved. You are being redirected to Home.";
		$redirect = "admin1.php?action=view_all";
	}elseif($sub_opt == "save_reg"){
		//open connection
		//same SQL commands
		//close connection
		echo "Your changes have been saved. You are being redirected to Registry.";
		$redirect = "updateRegistry.php";
	}

	header("Location: $redirect");
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

you can't send headers AFTER you've sent output (body). That is completely backwords. once you've echoed or printed ANYTHING to the screen, you can no longer modify header information.

If you need to display a message to the user while the page is redirecting, use a meta redirect... otherwise, just send the header and DONT output the message.

EDIT: also take a look here: viewtopic.php?t=1157

Although output buffering solves this problem, it is considered better practice to just do all* logic before any output is sent.

*template logic is OK... but let's just stick with ALL for the sake of clarity
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

i got it, thanks bro
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

i'm not sure why my sql commands aren't being executed on this redirect.php... anybody see anything wrong with the code? it's not bringing me any errors...

Code: Select all

<?php
	$uID        = $_POST['editID'];
	$sub_opt    = $_POST['sub_opt'];
	$brideFname = $_POST['brideFname'];
	$brideLname = $_POST['brideLname'];
	$groomFname = $_POST['groomFname'];
	$groomLname = $_POST['groomLname'];
/**************************************
	$ship_add   = $_POST['ship_add'];
	$ship_city  = $_POST['ship_city'];
	$ship_state = $_POST['ship_state'];
	$ship_zip   = $_POST['ship_zip'];
**************************************/
	$_POST['event_month'];
	$_POST['event_day'];
	$_POST['event_year'];
	
	if($_POST['event_month2'] != NULL){
		$event_month = $_POST['event_month2'];
	}if($_POST['event_day2'] != NULL){
		$event_day = $_POST['event_day2'];
	}if($_POST['event_year2'] != NULL){
		$event_year = $_POST['event_year2'];
	}
	
	if($sub_opt == "cancel"){
		$redirect = "admin1.php?action=view_all";
		header("Location: $redirect");
		echo "<CENTER>All changes were cancelled. You are being redirected to Home.</CENTER>";
	}elseif($sub_opt == "save"){
		@ $db = mysql_connect("yah", "blah", "blah");
		mysql_select_db("my_DB", $db);
		if(!$db){
			echo "Error: Could not connect to the database. Please try again later.";
			exit;
		}
		$sql = "INSERT INTO my_search_table where uID = '". $uID ."' VALUES('NULL', '$brideFname', '$brideLname', ".
			 "'$groomFname', '$groomLname', '$event_month', '$event_day', '$event_year', 'NULL', 'NULL', 'NULL', 'NULL'");
		mysql_query($sql);
		mysql_close($db);

		$redirect = "admin1.php?action=view_all";
		header("Location: $redirect");
		echo "<CENTER>Your changes have been saved. You are being redirected to Home.";
	}elseif($sub_opt == "save_reg"){
		@ $db = mysql_connect("yah", "blah", "blah");
		mysql_select_db("my_DB", $db);
		if(!$db){
			echo "Error: Could not connect to the database. Please try again later.";
			exit;
		}
		$sql = "INSERT INTO my_search_table where uID = '$uID' VALUES('NULL', '$brideFname', '$brideLname', ".
			 "'$groomFname', '$groomLname', '$event_month', '$event_day', '$event_year'");
		mysql_query($sql);
		mysql_close($db);		$redirect = "updateRegistry.php";
		header("Location: $redirect");
		echo "Your changes have been saved. You are being redirected to Registry.";
		$redirect = "updateRegistry.php";
	}
?>
Last edited by boo_lolly on Wed Nov 29, 2006 4:46 pm, edited 1 time in total.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

What happens when you execute it? I see you're still echoing before sending headers... use this function to redirect:

Code: Select all

<?php
function http_redirect($location)
{
    if(!headers_sent())
    {
        header('Location: ' . $location);
    }
    else
    {
        /**
         * Maybe put a meta redirect or echo out a link to click on here... or maybe 
         * even a javascript redirect... or just exit with a message saying "error - headers already sent"
         */
    }
    exit;
}
?>
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

The Ninja Space Goat wrote:What happens when you execute it? I see you're still echoing before sending headers... use this function to redirect:

Code: Select all

<?php
function http_redirect($location)
{
    if(!headers_sent())
    {
        header('Location: ' . $location);
    }
    else
    {
        /**
         * Maybe put a meta redirect or echo out a link to click on here... or maybe 
         * even a javascript redirect... or just exit with a message saying "error - headers already sent"
         */
    }
    exit;
}
?>
the headers are working fine, ninja. they're redirecting to the exact place they're supposed to. but the whole purpose of this redirect.php page is so that i can execute some SQL commands (replacing data in the SQL table), and the page they are directed to will display the new results. what's the issue? is there something wrong with my SQL command?


btw, TOOL is the best band in the history of the world.
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

i figured out the problem... i was missing a ')' in a couple of places. but now it gives me a parse error on line 37... and i don't see what i'm doin wrong...

Code: Select all

<?php
//LINE 37
		$sql = "INSERT INTO my_search_table where uID = '". $uID ."' VALUES('NULL', '$brideFname', '$brideLname', '$groomFname', '$groomLname', '$event_month', '$event_day', '$event_year', 'NULL', 'NULL', 'NULL', 'NULL'");
?>
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Code: Select all

<?php 
//LINE 37 
                $sql = "INSERT INTO my_search_table where uID = '". $uID ."' VALUES('NULL', '$brideFname', '$brideLname', '$groomFname', '$groomLname', '$event_month', '$event_day', '$event_year', 'NULL', 'NULL', 'NULL', 'NULL'"); 
?>
Why have you got a WHERE clause in an INSERT?
Is uID the primary key? Assuming it, make it of the form (for clarity)...

Code: Select all

$sql="INSERT into my_search_table (column1,column2,...,uID) VALUES('value1','value2'...,'$uID')";
$uID's value will be parsed into $sql because of the double quotes.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

')" not '")

But this will not work either. There is not WHERE clause for an INSERT statement.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

boo_lolly wrote:TOOL is the best band in the history of the world.
w00t! I'm listening to <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> right now! He's just about to say "And if when I say I might fade like a sigh if I stayeeyay! You minimize my movement anywaaaaayyy! I must persuade you another waaaaaaaaaaaaaaaaaaayyyyyyyy!"
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

pushin shuvin pushin me. THERE'S NO LOVE IN FEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAAAAR!!!!!!!!


hey volka, you're right. what i meant to do was UPDATE. not INSERT. good call bud. so how would i do all that insert stuff as an UPDATE command?? this is what i've got so far... but it still gives me the same error...

Code: Select all

$sql = "UPDATE my_search_table WHERE uID = '". $uID ."' ".
 "SET(brideFname, brideLname, groomFname, groomLname, event_month, event_day, event_year)".
 " = ('$brideFname', '$brideLname', '$groomFname', '$groomLname', '$event_month', '$event_day', '$event_year')";
mysql_query($sql) or die(mysql_error());
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Try

Code: Select all

$sql="UPDATE my_search_table SET brideFname='$brideFname', brideLname= '$brideLname',groomFname=$groomFname', groomLname='$groomLname', event_month='$event_month', event_day='$event_day', event_year='$event_year' WHERE uID=$uID";
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

boo_lolly wrote:pushin shuvin pushin me. THERE'S NO LOVE IN FEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAAAAR!!!!!!!!


hey volka, you're right. what i meant to do was UPDATE. not INSERT. good call bud. so how would i do all that insert stuff as an UPDATE command?? this is what i've got so far... but it still gives me the same error...

Code: Select all

$sql = "UPDATE my_search_table WHERE uID = '". $uID ."' ".
 "SET(brideFname, brideLname, groomFname, groomLname, event_month, event_day, event_year)".
 " = ('$brideFname', '$brideLname', '$groomFname', '$groomLname', '$event_month', '$event_day', '$event_year')";
mysql_query($sql) or die(mysql_error());
Replace function will do that for you.

Code: Select all

REPLACE INTO `tbl_name` SET `field_name`= ''
http://dev.mysql.com/doc/refman/5.0/en/replace.html
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

After using Header('Location=xyz'); it is good practice to always place an exit after and to always use the full path.
Example:

Code: Select all

if (!empty($redirect)) {
   header('Location:http://www.mysite.com/'.$redirect);
   exit;
}

// Do something else
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

hey everyone, thanks for all the great advice!! it's helped a lot. i've got an exit; after my redirects. the SQL error now is just a syntax error, which is better than before. but i've got ANOTHER problem... for some odd reason, my $_POST[''] commands aren't working. it's weird, they're just not being sent to the next page... i have no idea why. they're being sent the same way all my data has been sent to other pages... but it just won't work for this one... and the worst part about it is that the page that the information is coming from is an admin editing page... when the admin clicks 'SAVE' it takes them to 'redirect.php' which runs all the SQL queries. but the new editions aren't being posted either... it's just taking the same information from before the editions were made to the data....

the first thing i need to do is figure out the syntax error...
You have an error in your SQL syntax near 'WHERE uID = '' SET brideFname = '', brideLname = '', groomFname = '' groomLname ' at line 1

my SQL command

Code: Select all

$sql = "UPDATE my_search_table WHERE uID = '". $uID ."' ".
"SET brideFname = '$brideFname', brideLname = '$brideLname', groomFname = '$groomFname' ".
"groomLname = '$groomLname', event_month = '$event_month', event_day = '$event_day' ".
"event_year = '$event_year'";
what am i doing wrong? do you think it's giving me this error because the values for all of these haven't been $_POST'ed?
Post Reply