2015-04-06 103 views
0

我使用php將csv文件導入到我的數據庫(Mysql)中。 下面的代碼只將第一行(csv文件中的數據)插入到數據庫中。但它應該在數據庫中插入多行。通過php將csv文件導入到mysql中

$info = pathinfo($_FILES['file']['name']); 

/** 
* This checks if the file is a csv file 
*/ 
if(strtolower($info['extension']) == 'csv'){ 
    $filename=$_FILES["file"]["tmp_name"]; 

    if($_FILES["file"]["size"] > 0){ 

     //Open the file 
     $file = fopen($filename, "r"); 

     while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE){ 
      $num = count($emapData); 

      /** 
      * Insert data into the database if the columns are 
      * exactly in three columns 
      */ 
      if ($num == 3){ 
       $sql = "INSERT 
         INTO Users (firstName,surName,indexNo) 
        VALUES('$emapData[0]','$emapData[1]','$emapData[2]')"; 

       $result=$db->query($sql); 


       if($result){ 
        echo "<script type=\"text/javascript\"> 
          alert(\"CSV File has been successfully Imported.\"); 
          window.location = \"../administrator/bulkStudentReg.php\" 
         </script>"; 
       } 
       else{ 
        echo "<script type=\"text/javascript\"> 
        alert(\"Please Upload CSV File was not successful.\"); 
        window.location = \"../administrator/bulkStudentReg.php\" 
        </script>"; 
       } 
      } 
      else{ 
       echo "<script type=\"text/javascript\"> 
        alert(\"UPLOAD FAILED: Please your csv file contains incomplete column/s. Column should be 3 columns\"); 
        window.location = \"../administrator/bulkStudentReg.php\" 
        </script>"; 
      } 
     fclose($file); 
     } 
    } 
} 
else{ 
    echo "<script type=\"text/javascript\"> 
        alert(\"UPLOAD FAILED: Please the file should be in a csv file, eg. students.csv \"); 
        window.location = \"../administrator/bulkStudentReg.php\" 
        </script>"; 
} 
+0

什麼是$ emapData?它有什麼樣的數據? – 2015-04-06 11:24:49

回答

3

你在找這樣的事情。

我沒有測試,因爲我沒有來自你的樣品數據,但請給它一個去。

<?php 
$info = pathinfo($_FILES['file']['name']); 

/** 
* This checks if the file is a csv file 
*/ 
if(strtolower($info['extension']) == 'csv'){ 
    $filename=$_FILES["file"]["tmp_name"]; 

    if($_FILES["file"]["size"] > 0){ 

     //Open the file 
     $file = fopen($filename, "r"); 

     while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE){ 
      $num = count($emapData); 

      /** 
      * Insert data into the database if the columns are 
      * exactly in three columns 
      */ 
      if ($num == 3){ 
       $sql = "INSERT 
         INTO Users (firstName,surName,indexNo) 
        VALUES('$emapData[0]','$emapData[1]','$emapData[2]')"; 

       $result=$db->query($sql); 
      } 
     } 
     if($result){ 
        echo "<script type=\"text/javascript\"> 
          alert(\"CSV File has been successfully Imported.\"); 
          window.location = \"../administrator/bulkStudentReg.php\" 
         </script>"; 
       }else{ 
        echo "<script type=\"text/javascript\"> 
        alert(\"Please Upload CSV File was not successful.\"); 
        window.location = \"../administrator/bulkStudentReg.php\" 
        </script>"; 
       } 
    } 
    fclose($file); 
} 
else{ 
    echo "<script type=\"text/javascript\"> 
        alert(\"UPLOAD FAILED: Please the file should be in a csv file, eg. students.csv \"); 
        window.location = \"../administrator/bulkStudentReg.php\" 
        </script>"; 
} 
?> 
+0

是的,這個效果很好..但是,請問,我可以上傳每個請求的最大數量是多少。謝謝 – George 2015-04-06 11:53:00

+0

這取決於你的需要。顯然,沒有最大數量需要上傳每個請求。它會適合你的情況。 – 2015-04-06 12:13:36

4

你可以嘗試使用MySQL內置函數用於插入CSV數據,如果數據結構等於你的表結構也很簡單:

$sql = "LOAD DATA INFILE '$filename' INTO TABLE Users 
FIELDS TERMINATED BY ','" 

檢查MySQL LOAD DATA Documentation更多細節