Page 1 of 1

Warning: Cannot modify header information - headers alre...

Posted: Tue Jun 13, 2006 3:01 pm
by biggie
arborint | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a simple form in a html with a process.php file like:

Code: Select all

<?php
include("global.inc.php");
$errors=0;
$error="Some error message.<ul>";
pt_register('POST','CNP');
pt_register('POST','Nume');
pt_register('POST','Prenume');
if($CNP==" "){
$errors=1;
$error.="<li>Error...";
}
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$Email)){
$error.="<li>invalid email address";
$errors=1;
}
if($errors==1) echo $error;
else{
$link = mysql_connect("localhost","user","password");
mysql_select_db("db",$link);

$idtest=$_POST[id];
$quer = "SELECT id FROM pasagger WHERE id=$idtest";
$rez = mysql_query($quer);

if(mysql_num_rows($rez)) {
 echo "Duplicate ID";
} else {

$query="insert into pasager (cnp,nume,prenume) values ('".$CNP."','".$Nume."','".$Prenume."')";
mysql_query($query);
}
header("Location: http://www.google.com/");
?><?php 
}
?>


as u can see there is a if testing for duplicate id...if in the form I put an id that already is in the database then I will get the error (Duplicate ID) but under I also get

Warning: Cannot modify header information - headers already sent by (output started at c:\program files\nusphere\techplat\apache\htdocs\site\pazbor.php:31) in c:\program files\nusphere\techplat\apache\htdocs\site\pazbor.php on line 33(the line with header(...)


why? what am I doing wrong?


arborint | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Jun 13, 2006 3:05 pm
by Christopher
I suspect looking at the code that it is the line:

Code: Select all

echo "Duplicate ID";

Posted: Tue Jun 13, 2006 8:29 pm
by tecktalkcm0391
But your page on another page like includedpage.php
and than put this on yourpage you have now

Code: Select all

<?php
ob_start();
include("includedpage.php");
ob_flush();
?>


That should solve the message no matter what but you can try doing this first (on your current page):

Code: Select all

<?php
ob_start();

// Codes 

ob_flush();
?>

The ob_start(); I used on both examples, starts putting the codes into the memory, and then the ob_flush(); sends all of the items in memory to the client. ob_flush is called automatically at the end of every php code, but its better just to call it and be on the safe side.

Posted: Tue Jun 13, 2006 8:31 pm
by tecktalkcm0391
Also with:

Code: Select all

if(mysql_num_rows($rez)) { 
 echo "Duplicate ID"; 
} else {
It would be a little safer just do go ahead and make an arrugment:

Code: Select all

if(mysql_num_rows($rez)>0) { 
 echo "Duplicate ID"; 
} else {

Posted: Tue Jun 13, 2006 9:21 pm
by feyd
Tecktalkcm0391, suggesting output buffering is a band-aid approach to fixing "header already sent" issues. It's just not suggested, let alone just hiding the real problem.

It'd be a good idea to read this thread: viewtopic.php?t=1157