通过PHP将CSV文件导入MySQL

乔治

我正在使用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>";
}
保持编码

您在寻找这样的东西吗?

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章