I have successfully run this php code using my laptop, but it failed when I tried it on a server.
The purpose of this file is to download table (SQL) into .xls format.
<?php
session_start();
// Set name of excel doc
if(isset($_POST['t'])){
$setExcelName = $_POST['t'];
}else{
$setExcelName = 'Semua permohonan';
}
if(isset($_SESSION['demo']) AND $_SESSION['demo'] = 'yes'){
$demo = ' (DEMO)';
}else{
$demo = '';
}
include'connect.php';
// Define header / field name
$sql = "SHOW COLUMNS FROM senaraipermohonan";
$result = $conn->query($sql);
$setMainHeader = '';
while($row = $result->fetch_assoc()){
$rowname = '';
$rowname = '"'.$row['Field'].'"';
$setMainHeader .= $rowname . "\t";
}
// Set record in table
if(isset($_POST['q'])){
$sql = $_POST['q'];
}else{
$sql = "SELECT * FROM senaraipermohonan";
}
$record = $conn->query($sql);
if($record->num_rows >0){
$setData = '';
while ($row = $record->fetch_row()) {
$rowData = '';
foreach ($row as $value) {
$value = '"' . $value . '"' . "\t";
$rowData .= $value;
}
$setData .= $rowData . "
";
}
}else{
$setData = "Tiada data";
}
//This Header is used to make data download instead of display the data
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Permohonan SMAN2017 - " . $setExcelName . $demo . ".xls");
header("Pragma: no-cache");
header("Expires: 0");
//It will print all the Table row as Excel file row with selected column name as header.
echo ucwords($setMainHeader)."
".$setData."
";
?>
It returns this:
Not Acceptable
An appropriate representation of the requested resource /download_as_excel.php could not be found on this server.
Additionally, a 406 Not Acceptable error was encountered while trying to use an ErrorDocument to handle the request.
What are the possible reaasons for this problem? Thank you.
Looks like the error occures on handling an error.
So you have two things to do:
For the second I would look for the database credentials (username, password and so on) I would expect the error there. I think the username, password and db name is not the same on your laptop and on a server.