2015-04-06 107 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更多细节