duanran3115 2019-05-01 12:20
浏览 44

如何发送新标头以在PHP中下载Excel中的MySQL表条目?

I am facing a problem on downloading the SQL table value. I initially tried in Local network it works fine but when i shifted to cloud hosting of php pages it give me error as follows:

Warning: Cannot modify header information - headers already sent by (output 
started at /storage/ssd4/739/9447739/public_html/admindashboardentry.php:85)
in /storage/ssd4/739/9447739/public_html/admindashboardentry.php on line 738
Warning: Cannot modify header information - headers already sent by (output 
started at /storage/ssd4/739/9447739/public_html/admindashboardentry.php:85)
in /storage/ssd4/739/9447739/public_html/admindashboardentry.php on line 739

but it displays the sql table on webpage below these warning.

while when i was working on Local network it works fine.

below some code i am using to download the sql table entries.

//EXCEL DOWNLOAD SET START
//if condition to check download button is pressed and start download the csv file

if(downloadExcel() === 1){

        $empid = $_POST['empid'];
        $projectcode = $_POST['projectcode'];
        $startdate = $_POST['startdate'];
        $enddate = $_POST['enddate'];                                                                                   

        // Connection 
        include('connection.php');

        $date = date('Y-m-d h:i:s A');

        $empName = getEmpNameById($empid);

        $filename = $empName." - ".$date.".xls"; // File Name

        // Download file
        header("Content-Disposition: attachment; filename=\"$filename\"");
        header("Content-Type: application/vnd.ms-excel");

        //table name in database is in lowercase as 'cip004'
        $empidTable = strtolower($empid);

        $user_query = "SELECT * FROM $empidTable WHERE EmpID = '$empid' AND ProjectCode = '$projectcode' AND Date BETWEEN '$startdate' AND '$enddate'";
        $result = $conn->query($user_query);

        tableHeading();

        //loop the query data to the table in same order as the headers
        while ($row = $result->fetch_assoc()){

            $status = statusOfEntry($row['Status']);
            tableRow($row, $status);

        }
        echo '</table>';

}

  • 写回答

2条回答 默认 最新

  • doucao8982 2019-05-02 06:53
    关注

    If you have error_reporting or display_errors disabled per php.ini, then no warning will show up. But ignoring errors won't make the problem go away. Headers still can't be sent after premature output.

    So when header("Location: ...") redirects silently fail it's very advisable to probe for warnings. Reenable them with two simple commands atop the invocation script:

    error_reporting(E_ALL);
    ini_set("display_errors", 1);
    

    Or set_error_handler("var_dump"); if all else fails.

    Preferably even a utility function, which prints a user message in case of header() failures.

    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭