I am generating a excel file my php using the following script.it gives me the following error.What may be the reason for this-
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\geochronology\library\admin\generate-excel.php:45) in C:\xampp\htdocs\geochronology\library\admin\generate-excel.php on line 56
<?php
// Database Connection file
include('includes/config.php');
?>
<table border="1">
<thead>
<tr>
<th>Sr.</th>
<th>User Name</th>
<th>Affiliation</th>
<th>Sample Type</th>
<th>Sample ID</th>
<th>Date</th>
</tr>
</thead>
<?php
// File name
$filename="Sample-summary";
// Fetching data from data base
        $search=$_POST['search2'];
        $option=$_POST['option2'];
        $period=$_POST['period2'];
        $datefrom=$_POST['datefrom2'];
        $dateto=$_POST['dateto2'];
        $dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
            if($period ==null){
            $sql = "SELECT tbluser.user,tbluser.affiliation,tblfacility.type,tblfacility.sampleid,tblfacility.time,DATE_FORMAT(tblfacility.time, '%d-%m-%y') AS formatted_date 
            FROM tblfacility 
            JOIN tbluser on tbluser.id=tblfacility.user 
            where ".$search." ='".$option."' ";}
            else{
            $sql="SELECT * FROM tblfacility JOIN tbluser on tbluser.id=tblfacility.user where ".$search." ='".$option."' AND time between '".$datefrom."' and '".$dateto."' ";
            }
            $query = $dbh->prepare($sql);
            $query->execute();
            $results=$query->fetchAll(PDO::FETCH_OBJ);
            //echo "<prep>";
            //echo "this is the final";
            print_r($sql);
            $cnt=1;         
            foreach($results as $result){
?>
            <tr>
                <td><?php echo $cnt;?></td>
                <td><?php echo $row['user'];?></td>
                <td><?php echo $row['affiliation'];?></td>
                <td><?php echo $row['type'];?></td>
                <td><?php echo $row['sampleid'];?></td>
                <td><?php echo $row['time'];?></td>
            </tr>
<?php
$cnt++;
// Genrating Execel  filess
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$filename."-Report.xls");
header("Pragma: no-cache");
header("Expires: 0");
            }
            ?>
</table>
 
     
     
    