gpt4 book ai didi

php - 如何在 PHP 中发送新 header 以下载 Excel 中的 MySQL 表条目?

转载 作者:行者123 更新时间:2023-11-29 16:03:46 24 4
gpt4 key购买 nike

我在下载 SQL 表值时遇到问题。我最初在本地网络中尝试过,它工作正常,但是当我转移到 php 页面的云托管时,它给我错误如下:

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

但它在网页上显示这些警告下方的 SQL 表。

当我在本地网络上工作时,它工作正常。

下面是我用来下载 sql 表条目的一些代码。

//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>';

}

最佳答案

如果您在 php.ini 中禁用了 error_reporting 或 display_errors,则不会显示任何警告。但忽略错误并不会让问题消失。过早输出后仍然无法发送 header 。

因此,当 header("Location: ...") 重定向无提示地失败时,建议探测警告。使用调用脚本顶部的两个简单命令重新启用它们:

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

或者set_error_handler("var_dump");如果一切都失败了。

最好是一个实用函数,在 header() 失败的情况下打印用户消息。

关于php - 如何在 PHP 中发送新 header 以下载 Excel 中的 MySQL 表条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55936192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com