的Excel到MySQL使用PHP

问题描述:

我不得不Excel文件记录上传到mysql,我写了下面的代码是工作的罚款...的Excel到MySQL使用PHP

<?php 

ini_set("display_errors",1); 
require_once 'excel_reader2.php'; 
require_once 'db.php'; 

$data = new Spreadsheet_Excel_Reader("file.xls"); 

echo "Total Sheets in this xls file: ".count($data->sheets)."<br /><br />"; 

$html="<table border='1'>"; 
for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file. 
{ 
    if(count($data->sheets[$i][cells])>0) // checking sheet not empty 
    { 
     echo "Sheet $i:<br /><br />Total rows in sheet $i ".count($data->sheets[$i][cells])."<br />"; 
     for($j=1;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of the sheet 
     { 
      $html.="<tr>"; 
      for($k=1;$k<=count($data->sheets[$i][cells][$j]);$k++) // This loop is created to get data in a table format. 
      { 
       $html.="<td>"; 
       $html.=$data->sheets[$i][cells][$j][$k]; 
       $html.="</td>"; 
      } 
     $Productcode = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][1]); 
      $Artikel = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][2]); 
      $EANcode = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][3]); 
      $url_kiesk_nl = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][4]); 

      $query = "insert into test(Productcode,Artikel,EANcode,url_kiesk_nl) values('".$Productcode."','".$Artikel."','".$EANcode."','".$url_kiesk_nl."')"; 

      mysqli_query($connection,$query); 
      $html.="</tr>"; 
     } 
    } 

} 

$html.="</table>"; 
echo $html; 
echo "<br />Data Inserted in dababase"; 
?> 

现在我有一个单词添加到了第4列每条记录都写了这条线。其实我的要求是给第四列的每个单元格添加“1”。

$url_kiesk_nl = mysqli_real_escape_string($connection,$data->"1".sheets[$i][cells][$j][4]); 

但这不工作...任何人都可以请帮助我吗?

使用此:

$url_kiesk_nl = "1".mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][4]); 

或者这样:

$url_kiesk_nl = mysqli_real_escape_string($connection,"1".$data->sheets[$i][cells][$j][4]); 
+0

感谢,但同时该表显示第四列即将空白 – user3305327