2015-02-09 47 views
0

我要出口被稱爲「lokasi」到Excel的MySQL表,但這些都是我得到的錯誤:PHPExcel錯誤:未定義指數

Notice: Undefined index: no_lokasi in C:\xampp\htdocs\inventory\pages\report.php on line 14

Notice: Undefined index: nama_lokasi in C:\xampp\htdocs\inventory\pages\report.php on line 15

Notice: Undefined index: keterangan_lokasi in C:\xampp\htdocs\inventory\pages\report.php on line 16

Notice: Undefined index: no_lokasi in C:\xampp\htdocs\inventory\pages\report.php on line 14

Notice: Undefined index: nama_lokasi in C:\xampp\htdocs\inventory\pages\report.php on line 15

這裏是我的代碼:

<?php 
    if($_SESSION["role"]!=1){ 
     header('location:login.php'); 
    } 
    else { 
     require_once dirname(__FILE__) . '\..\bower_components\phpexcel\Classes\PHPExcel.php'; 

     if(isset($_POST["submit"])){ 

      $objPHPExcel = new PHPExcel(); 
      $objWorksheet = $objPHPExcel->setActiveSheetIndex(0); 

      $result = mysql_query("SELECT * FROM lokasi",$conn); 

      $rowCount= 1; 
      while($row=mysql_fetch_row($result)){ 
       $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['no_lokasi']); 
       $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['nama_lokasi']); 
       $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row['keterangan_lokasi']);    
       $rowCount++; 
      } 

      $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
      $objWriter->save('some_excel_file.xlsx'); 
     } 
?>  
     <div class="row"> 
      <div class="col-lg-12"> 
       <h1 class="page-header">Print Preview</h1> 
      </div> 
     </div> 
     <div class="row"> 
      <div class="col-lg-12"> 
       <div class="panel panel-default"> 
        <div class="panel-body"> 
         <div class="dataTable_wrapper"> 
          <table class="table table-striped table-bordered table-hover" id=""> 
           <thead> 
            <tr> 
             <center> 
              <th>Nama Lokasi</th> 
              <th>Keterangan</th> 
              <th>Action</th> 
             </center> 
            </tr> 
           </thead> 
           <tbody> 
           <?php 
            $result = mysql_query('SELECT * from lokasi'); 
            while($list = mysql_fetch_row($result)){ 
             echo' 
              <tr class="odd gradeA"> 
               <td>'.$list[0].'</td> 
               <td>'.$list[1].'</td> 
               <td>'.$list[2].'</td>  
              </tr>   
             '; 
            } 
           ?>            
           </tbody> 
          </table> 
         </div> 
         <div class="row"> 
          <div class="col-lg-6"> 
           <form role="form" method="post"> 
            <button type="submit" class="btn btn-primary" name="submit">Submit</button> 
           </form> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 
     </div> 
<?php 
    }; 
?> 
+0

難道不明顯嗎?檢查你的查詢結果的內容。 – 2015-02-09 04:28:17

+0

似乎你的桌子上沒有這些欄目。 – 2015-02-09 04:38:25

回答

0

您正在使用和mysql_fetch_row來獲取結果,你是索引這樣

while($row=mysql_fetch_row($result)){ 
       $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['no_lokasi']); 
       $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['nama_lokasi']); 
       $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row['keterangan_lokasi']);    
       $rowCount++; 
} 

更改爲

while($row=mysql_fetch_row($result)){ 
       $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row[0]); // to corresponding row indexes 
       $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row[1]); 
       $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row[2]);    
       $rowCount++; 
} 

因爲mysql_fetch_row()函數從結果集中獲取一行並將其作爲枚舉數組返回。

+0

感謝您的幫助:D – Oiy 2015-02-09 05:01:23