【问题标题】:Import csv file into mysql via php通过php将csv文件导入mysql
【发布时间】:2015-06-10 19:00:00
【问题描述】:

我正在使用 php 将 csv 文件导入我的数据库 (Mysql)。 下面的代码仅将第一行(csv 文件中的数据)插入数据库。但它应该在数据库中插入多行。

$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>";
}

【问题讨论】:

  • 什么是$emapData?里面有什么样的数据?

标签: php mysql csv


【解决方案1】:

您可以尝试使用 MySQL 内置函数插入 CSV 数据,如果您的数据结构与您的表结构相同,则非常简单:

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

查看MySQL LOAD DATA Documentation了解更多详情

【讨论】:

    【解决方案2】:

    您是否正在寻找这样的东西。

    我没有测试是因为我没有你的样本数据,但请试一试。

    <?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>";
    }
    ?>
    

    【讨论】:

    • 是的,这很好用。但是请,我可以为每个请求上传的最大数量是多少。谢谢
    • 这取决于您的需要。显然没有每个请求需要上传的最大数量。它适用于您的情况。
    猜你喜欢
    • 2013-08-30
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 2019-03-14
    相关资源
    最近更新 更多