-1

执行下面的代码时,它返回错误“警告无法修改标头信息 - 标头已由.....发送”。

我已经编写了数组以两种不同的方式提取数据,但仍然收到问题。

<?php  
//export.php  
    $connect = mysqli_connect("localhost", "username", "password", "dbname");
    $output = '';
    if(isset($_POST["export"]))
{
        $query = "
        select trim(ifnull(application, 'Grand Total' )) as Application, 
        ifnull(sum(Totalcapacity) ,0.00) as 'Capacity (Gig)'
        from storagedata group by application with rollup
         ";

        $result1 = mysqli_query($connect, $query);

        $result2 = mysqli_query($connect, $query);

        $dataRow = "";

        while($row2 = mysqli_fetch_array($result2))

{
        $dataRow = $dataRow."<tr><td>$row2[0]</td><td>$row2[1]</td></tr>";

}
?>



<html>
<head>

        <title>Returning Total Capacity Per DC</title>

</head>

<body>

<!-- Building table -->
        <table align = 'center' border='2' width='300' height='100'>
            <tr>
            <td colspan='2' align='center' bgcolor='lightblue'><b>Total Capacity by DC</b> </td>
            </tr>
            <tr>
                <th>Datacenter</th>
                <th>Total Capacity (Gig)</th>

            </tr>

            <?php while($row1 = mysqli_fetch_array($result1)):;?>
            <tr>
                <td><?php echo $row1[0];?></td>
                <td><?php echo $row1[1];?></td>

            </tr>
            <?php endwhile;?>
</body>
     <br><br>




<?php

  $output .= '</table>';
  header('Content-Type: application/xlsx;');
  header('Content-Disposition: attachment; filename=download.xlsx');
  echo $output;

  }

?>

从我的导出按钮单击时,它应该导出数据。同样的代码也适用于其他“报告”。唯一的区别是顶部的选择查询。

4

2 回答 2

0

地方:

header('Content-Type: application/xlsx;');
header('Content-Disposition: attachment; filename=download.xlsx');

在脚本的开头。

<?php
header('Content-Type: application/xlsx;');
header('Content-Disposition: attachment; filename=download.xlsx');

//...

并确保

<?php

位于文件的开头,之前没有行或空格。

在您的脚本发送了一些输出之后,您将无法更改标头设置。

于 2019-05-21T10:45:41.567 回答
-1

将此代码放在警告消息中签名的 PHP 文件的顶部。

<?php ob_start(); ?>
于 2019-05-21T10:31:19.050 回答