2016-11-17 87 views
0

我已經成功地運行這個php代碼使用我的筆記本電腦,但它失敗了,當我在服務器上嘗試它。406不能接受的錯誤下載表爲xls

該文件的目的是將表(SQL)下載到.xls格式。

<?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 . "\n"; 
     } 
    }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)."\n".$setData."\n"; 


?> 

它返回:

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. 

哪些這個問題可能reaasons?謝謝。

回答

0

看起來像處理錯誤時發生錯誤。

所以,你有兩兩件事要做:

  • 找到錯誤處理錯誤文件。
  • 找到原來的錯誤

對於第二個我會找數據庫憑據(用戶名,密碼等),我希望有錯誤。我認爲你的筆記本電腦和服務器上的用戶名,密碼和db名稱是不一樣的。