Page 1 of 1

Header Problem in PHP

Posted: Thu May 20, 2004 8:33 am
by Gopi
When storing the details received from Html page, I got this error message.


Warning: Cannot modify header information - headers already sent by (output started at D:\domains\cddda.com\wwwroot\hichennai\savestepone.php:2) in D:\domains\cddda.com\wwwroot\hichennai\savestepone.php on line 60


The code i use is given below :



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
include 'global.php';
$sql="select * from canddetails where uname='" . $_POST['txtuname'] . "'";
$conn=mysql_connect($localhost,$myuser,$mypwd);
$db=mysql_select_db($dbname,$conn);
$result=mysql_query($sql,$conn);
$norows=mysql_num_rows($result);
$flag=false;
if (isset($_GET['txtNocomp'])==true)
{
if ($_GET['txtNocomp']=="")
{
$nocomp="";
$flag=false;
}
else
{
$nocomp=$_GET['txtNocomp'];
$flag=true;
}
}
$uname=$_POST['txtuname'];
$candname=$_POST['txtcandname'];
$dob=$_POST['cmbdate'] . "/" . $_POST['cmbmonth'] . "/" . $_POST['cmbyear'];
$age =$_POST['txtage'];
$address =$_POST['txtaddr'];
$city=$_POST['txtcity'];
$state=$_POST['txtstate'];
$phoneno=$_POST['txtphonecode'] . "-" . $_POST['txtphoneno'];
$email=$_POST['txtemail'];
$totexp=$_POST['txttotexp'];
$qualification=$_POST['txtqualification'];
$jobcategory=$_POST['txtjobcategory'];
$country=$_POST['txtcountry'];
$lang=$_POST['txtlangknown'];
//$norec=$_GET['norec'];
if ($norows>0)
{
$sql="update canddetails set uname='" . $uname . "',candname='" . $candname . "',dateofbirth ='" . $dob . "',age=" . $age . ",address='" . $address . "',city='" . $city . "',state='" . $state . "',phoneno'" . $phoneno . "',email='" . $email . "',totexp=" . $totexp . ",qualification='" . $qualification . "',jobcategory='" . $jobcategory . "',country='". $country . "',langknown='" . $lang . "')";
$result=mysql_query($sql,$conn);
if ($nocomp != "")
{
header("Location: previous_exp.php?txtNocomp=". $nocomp . "&txtuname=" . $_POST['txtuname']);
exit();
}
else
{
header("Location: previous_exp.php?txtuname=" . $_POST['txtuname']);
exit();
}
}
else
{
if ($flag==false)
{
$sql="insert into canddetails(uname,candname,dateofbirth,age,address,city,state,phoneno,email,totexp,qualification,jobcategory,country,langknown) values('" . $uname . "','" . $candname . "','" . $dob . "'," . $age . ",'" . $address . "','" . $city . "','" . $state . "','" . $phoneno . "','" . $email . "'," . $totexp . ",'" . $qualification . "','" . $jobcategory . "','". $country . "','" . $lang . "')";
$result=mysql_query($sql,$conn);
header("Location: previous_exp.php?txtuname=" . $_POST['txtuname']);
exit();
}
else
{
$sql="insert into canddetails(norec,uname,candname,dateofbirth,age,address,city,state,phoneno,email,totexp,qualification,jobcategory,country,langknown) values(" . $nocomp . ",'" . $uname . "','" . $candname . "','" . $dob . "'," . $age . ",'" . $address . "','" . $city . "','" . $state . "','" . $phoneno . "','" . $email . "'," . $totexp . ",'" . $qualification . "','" . $jobcategory . "','". $country . "','" . $lang . "')";
$result=mysql_query($sql,$conn);
if ($nocomp != "")
{
header("Location: previous_exp.php?txtNocomp=". $nocomp . "&txtuname=" . $_POST['txtuname']);
exit();
}
else
{
header("Location: previous_exp.php?txtuname=" . $_POST['txtuname']);
exit();
}
}
}
?>

Posted: Thu May 20, 2004 8:42 am
by delorian
If you are using header() function you should not send anything to the browser before, like here you send the following line:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

http://php.net/header

Posted: Thu May 20, 2004 8:45 am
by leenoble_uk
You're using the Header() function to redirect to another page AFTER you have already sent some information to the client.
You've already sent the DOCTYPE information. You can't send ANY information at all before declaring a header.
You'll have to restructure your document to not send out the DOCTYPE until a decision is made on which page is being sent.

Posted: Thu May 20, 2004 8:46 am
by leenoble_uk
curse my inadequate slow typeage!

Posted: Thu May 20, 2004 8:52 am
by delorian
leenoble_uk wrote:curse my inadequate slow typeage!
[Off Topic]: Happens all the time :P

Posted: Thu May 20, 2004 8:58 am
by JayBird
Read this The complete solution to your question is...

Then read this Warning: Cannot add header information

Take notes, and use what you learn.

Mark