Log in

View Full Version : header() creating problem



aqeel
09-27-2010, 10:07 AM
Hi guys im using the following code query is executing n creating records in database but the page don't redirect to content.php. following is the code with the errir statement
<?php
$menuName = $_POST['menuName'];
$position = $_POST['position'];
$visibilty = $_POST['visibilty'];

$query = "INSERT INTO subjects(
menuName, position , visibilty
) VALUES(
'{$menuName}',{$position},{$visibilty}
)";

if(mysql_query($query, $conection)){
// succeded
//echo "Subject creation successful";
header("Location: content.php");
exit;
}else{
// failed
echo "<p>Subject creation failed</p>" ;
echo "<p>". mysql_error() ."</p>";
}
?>
The error stament is
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\widget\creat_subject.php

fastsol1
09-27-2010, 12:57 PM
You can't have any output to the browser before using the header(). If you need to have output beforehand then put this at the very top right after the opening php tag

ob_start();

djr33
09-27-2010, 04:57 PM
It should always be possible (except in very special circumstances) to avoid output buffers.

Headers must be sent before anything is output (including even blank lines) so just plan your structure accordingly.


For example, you can just reverse these two lines:

header("Location: content.php");
//echo "Subject creation successful";

aqeel
09-28-2010, 08:19 AM
thanks with regards
aqeel