i am trying to create a new subject and redirecting the page to the home page after the subject is created which is(content.php).

The code succesfully created the object but the page redirecting that i try to make is failed and my browser came out with this error message :

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\newcompany\create_subjects.php:15) in C:\xampp\htdocs\newcompany\create_subjects.php on line 29

and i could not figure out whats wrong here,this is the code from the page of create_subject.php :

<?php
$connection = mysql_connect("localhost","root","mypassword");
if (!$connection) {
  die ("database failed to connect " . mysql_error());
}

$db_select = mysql_select_db("newcompany");
if (!$db_select) {
  die ("database failed to connect " . mysql_error());
}
?>

<?php
	$menu_name = $_POST['menu_name'];
	$position = $_POST['position'];
	$visible = $_POST['visible'];
?>
<?php
	$query = "INSERT INTO subjects (
				menu_name, position, visible
			) VALUES (
				'{$menu_name}', {$position}, {$visible}
			)";
	$result = mysql_query($query, $connection);
	if ($result) {
		// Success!
                header("Location: content.php");
		exit;

	} else {
		// Display error message.
		echo "<p>Subject creation failed.</p>";
		echo "<p>" . mysql_error() . "</p>";
	}
?>


<?php
mysql_close($connection);
?>

could somebody help me please?

Thank You :)

A real simple and quick fix is to add this before you do the header():

ob_clean();

That cleans any output and will fix this in a snap. If you're concerned with figuring out the cause (and some people may think you should be), open up create_subjects.php and look around line 15 for anything that is getting output. Any echo commands before a header() call is a no-no. And maybe you have some white space outside of the opening or closing PHP tags.

Thanks for reply..
But that is not fix my problem by putting ob_clean(); as you suggested.

and the line 15 as you asked me to look at does not have any output instead it is only this :
$position = $_POST;

as you could see above from my original post..

I think line 12 is your problem... Outside of the closing PHP tag on line 11, you then go to a new line and then another new line (which is whitespace!) and open another PHP tag on line 13...

What this means is that the white space is output data, which breaks headers.. Headers should only be sent before any output is sent, and that white space on line 12 is the problem.

So instead, I would combine all your PHP code within one PHP tag, and that will fix any white space.

Give that a short and I'm sure it should go away - Let me know!

Here's an example of what I mean should fix the problem:

<?php
$connection = mysql_connect("localhost","root","mypassword");
if (!$connection) {
  die ("database failed to connect " . mysql_error());
}

$db_select = mysql_select_db("newcompany");
if (!$db_select) {
  die ("database failed to connect " . mysql_error());
}

	$menu_name = $_POST['menu_name'];
	$position = $_POST['position'];
	$visible = $_POST['visible'];

	$query = "INSERT INTO subjects (
				menu_name, position, visible
			) VALUES (
				'{$menu_name}', {$position}, {$visible}
			)";
	$result = mysql_query($query, $connection);
	if ($result) {
		// Success!
                header("Location: content.php");
		exit;

	} else {
		// Display error message.
		echo "<p>Subject creation failed.</p>";
		echo "<p>" . mysql_error() . "</p>";
	}

mysql_close($connection);
?>

The difference here is that there is no white space outside the PHP tags (which is sent to the browser).. Whitespace INSIDE a PHP tag is fine and dandy - it's just when it's outside the PHP tags that the browser interprets it as space.

owh...
Thank You sooo much SikoSoft :)

It tooks me hours b4 you helped me to solve this..

So,in any of the page that redirected using the header,it should be no white spaces between the PHP tag right?

is my conclusion is correct?

Yeah, that conclusion is correct. There shouldn't be any space outside (or between multiple) PHP tags, as this will be read as a space or new line in HTML, and any HTML (even if only a space) sent as output before headers causes the problem. It can be annoying as heck, but if you know about it it can be remedied pretty easily. I've had to deal with this a lot in some of my projects where I include lots and lots of files on each page load, and all it takes is a new line after the closing PHP tag in one of those included files to completely break functionality and throw these warnings.

Glad it helped!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.