Error redirecting when submitting data [SOLVED]

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

Post Reply
D_zone
Forum Newbie
Posts: 14
Joined: Mon Nov 02, 2009 9:22 am

Error redirecting when submitting data [SOLVED]

Post 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); ?>

Last edited by D_zone on Tue Apr 24, 2012 9:31 am, edited 1 time in total.
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Error redirecting when submitting data

Post 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.
D_zone
Forum Newbie
Posts: 14
Joined: Mon Nov 02, 2009 9:22 am

Re: Error redirecting when submitting data

Post 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?
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Error redirecting when submitting data

Post 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
Last edited by x_mutatis_mutandis_x on Fri Apr 20, 2012 3:05 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Error redirecting when submitting data

Post 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.
D_zone
Forum Newbie
Posts: 14
Joined: Mon Nov 02, 2009 9:22 am

Re: Error redirecting when submitting data [SOLVED]

Post 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.
Post Reply