Page 1 of 1

Error redirecting when submitting data [SOLVED]

Posted: Fri Apr 20, 2012 1:12 pm
by D_zone
I have this code, for some reason it is no redirecting to the address I'd like to, yet it shows a plain blank page after submitting data.
It does submit the info but it just won't redirect me to google.

Code: Select all

<?php

$connection = mysql_connect("localhost","root","root");
if (!$connection) {
	die("Database connection failed: " . mysql_error());
}


$db_select = mysql_select_db("data",$connection);
if (!$db_select) {
	die("Database selection failed: " . mysql_error());
}

 	$name = trim($_POST['name']);
	$last = trim($_POST['last']);
	$email = trim($_POST['email']);
	$password = trim($_POST['pass']);
	$password2 = trim($_POST['pass2']);

$query = "INSERT INTO staff_registered ( name, lastname, email, pass1, pass2 ) VALUES 
									('{$name}', '{$last}', '{$email}', '{$password}', '{$password2}' )";
	
	if(mysql_query($query, $connection)) {
		
		header("Location: http://www.google.com");
		exit;
	}else{
		echo "<p>ERROR</p>";
		echo "<p>" . mysql_error() . "</p>";
	}

?>


<?php 
mysql_close($connection); ?>


Re: Error redirecting when submitting data

Posted: Fri Apr 20, 2012 1:23 pm
by tr0gd0rr
first enable error display:

Code: Select all

ini_set('display_errors',1);
error_reporting(E_ALL);
Then let us know what it says.

Re: Error redirecting when submitting data

Posted: Fri Apr 20, 2012 1:44 pm
by D_zone
Thank you tr0gd0rr for replying;

I tried ini_set('display_errors',1); and it show this error

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ in /Applications/MAMP/htdocs/process.php on line 30
-------->header("Location: http://www.google.com"); <-------

What might be causing this problem?

Re: Error redirecting when submitting data

Posted: Fri Apr 20, 2012 2:04 pm
by x_mutatis_mutandis_x
The error usually means that response has been commited to the browser and you cannot resend headers. Usually if something gets echo-ed to the browser before header('Location...).
Try putting '@' before the mysql functions, like @mysql_connect, @mysql_select_db, @mysql_query

Re: Error redirecting when submitting data

Posted: Fri Apr 20, 2012 2:27 pm
by requinix
D_zone wrote:What might be causing this problem?
According to the error message you posted, whatever is on line 30 of process.php.

Re: Error redirecting when submitting data [SOLVED]

Posted: Tue Apr 24, 2012 9:28 am
by D_zone
I finally can fixed the problem. What I found out is that THIS is a very common error in PHP, I guess the spaces before the header tag might be one reason so I tried a different way to redirect instead to use header tag. Echo some java Script.

echo("<script>location.href = 'http://www.google.com';</script>");

That fixed the redirection problem.

Thank you everyone for your comments.