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>';
}
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.
I got the answer by making separate PHP file as download.php
and then calling it on form submission of download button.
Below are the content of download.php
file, i am posting here only one IF
condition as in question.
<?php
include('connection.php');
function statusOfEntry($number)
{
if($number==1){
return "Billable";
}elseif ($number == 2) {
return "Not Billable";
}elseif ($number == 3) {
return "Invoiced";
# code...
}elseif ($number == 4) {
return "UnInvoiced";
# code...
}elseif ($number == 0) {
return "Not Seen";
# code...
}
}
//Excel File Table Heading with Modified Date Column in a table
function tableHeading(){
echo '<table border="1">';
//make the column headers what you want in whatever order you want
echo '<tr><th>Emp ID - Name</th><th>Project Code</th><th>Date</th><th>Start Time</th><th>End Time</th><th>No of Hours</th><th>Task Performed</th><th>Modified Date</th><th>Status</th></tr>';
}
function tableRow($row, $status)
{
$empName = getEmpNameById($row['EmpID']);
echo "<tr><td>".$empName."</td><td>".$row['ProjectCode']."</td><td>".$row['Date']."</td><td>".$row['StartTime']."</td><td>".$row['EndTime']."</td><td>".$row['NoOfHours']."</td><td>".$row['TaskPerformed']."</td><td>".$row['ModifiedDate']."</td><td>".$status."</td></tr>";
}
function getEmpNameById($empid){
$filename = "C:/wamp64/www/test/empid.txt";
$fp = @fopen($filename, 'r') or die('Unable to open file!');
if($fp){
$empid_array = explode(PHP_EOL, fread($fp, filesize($filename)));
}
$empid_array = array_filter($empid_array);
$empid_array = array_unique($empid_array);
sort($empid_array);
$keyArray = array();
$valueArray = array();
$combined = [];
// passing $combined by reference so we can modify it
array_walk($empid_array, function ($e) use (&$combined) {
list($id, $name) = explode(" - ", $e);
$combined[$id] = $name;
});
return $empid." - ".$combined[$empid]." " ?? $empid;
}
function downloadExcel(){
if(isset($_POST['allsetdownload']))
{
// If condition to check all the checkbox is checked i.e. Empid, ProjectCode, StartDate, EndDate
$download = 1;
return $download;
}elseif(isset($_POST['threesetdownload'])){
// If condition to check all the checkbox is checked i.e. Empid, ProjectCode, EndDate
$download = 2;
return $download;
}elseif(isset($_POST['twosetdownload'])){
// If condition to check all the checkbox is checked i.e. Empid, ProjectCode
$download = 3;
return $download;
}elseif(isset($_POST['noprojectcodedownload'])){
// If condition to check all the checkbox is checked i.e. Empid, StartDate, Enddate
$download = 4;
return $download;
}elseif (isset($_POST['enddatedownload'])){
// If condition to check all the checkbox is checked i.e. Empid, Enddate
$download = 5;
return $download;
}elseif(isset($_POST['empiddownload'])){
// If condition to check all the checkbox is checked i.e. Empid
$download = 6;
return $download;
}elseif(isset($_POST['projectcodedownload'])){
//// If condition to check all the checkbox is checked i.e. Projectcode
$download = 7;
return $download;
}elseif(isset($_POST['statusentrydownload'])){
// If condition to check checkbox is checked i.e. status Billable or NotBillable or Invoiced or UnInvoiced.
$download = 8;
return $download;
}elseif (isset($_POST['projectCodeStartDateEndDate'])) {
//If condition to check for input, i.e. ProjectCode, StartDate, EndDate.
$download = 9;
return $download;
# code...
}elseif (isset($_POST['projectCodeStartDate'])) {
//If condition for input, i.e. ProjectCode, StartDate
$download = 10;
return $download;
# code...
}elseif (isset($_POST['projectCodeEndDate'])) {
//If condition for input, i.e. ProjectCode, EndDate
$download = 11;
return $download;
# code...
}
}
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");
$user_query = "SELECT * FROM $empid 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>';
}
?>
Below is the if condition of file admindashboardentry.php
whose download button calls download.php
. Actually initially i was trying to call all the functionality of download.php
in same admindashboardentry.php
on button click.
But somehow i separated the code and made new file download.php
for download click and send new header in PHP for downloading the Excel sheet of SQL Table and Made things working wow.
if(isset($_POST['empidcheck']) and isset($_POST['projectcodecheck']) and isset($_POST['startdatecheck']) and isset($_POST['enddatecheck'])){
$empid = $_POST['empid'];
$projectcode = $_POST['projectcode'];
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];
//to make database connection
include('connection.php');
echo "<p>Admin Entry Received: $empid, $projectcode, $startdate, $enddate</p>";
echo "<form action = 'download.php' method = 'post'>";
echo "<input type ='hidden' name='empid' value= '$empid' >";
echo "<input type ='hidden' name='projectcode' value= '$projectcode' >";
echo "<input type ='hidden' name='startdate' value= '$startdate' >";
echo "<input type ='hidden' name='enddate' value= '$enddate' >";
echo "<input type='submit' name = 'allsetdownload' value = 'Download'>";
echo "</form>";
echo "<br>";
//To Display Name and Employee ID above table Start
$nameDisplaySql = "SELECT EmpID, Name FROM employee_data WHERE EmpID = '$empid'";
$result = $conn->query($nameDisplaySql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<p>".$row["EmpID"]." - ".$row["Name"]."</p>";
echo "<hr>";
}
}
//To Display Name and Employee ID above table End
$selectSql = "SELECT * FROM $empid WHERE EmpID = '$empid' AND ProjectCode = '$projectcode' AND Date BETWEEN '$startdate' AND '$enddate' ORDER BY Date DESC, ModifiedDate DESC";
$result = mysqli_query($conn, $selectSql);
$sumNoOfHours = 0.0;
displayTableHeading();
while($row = mysqli_fetch_array($result))
{
$sumNoOfHours = $sumNoOfHours + $row['NoOfHours'];
displayTableRow($row);
}
tableEnding($sumNoOfHours);
}