0

I am importing a csv file into my database (Mysql) using php. This code below inserts only the first row (data in the csv file) into the database. But it is supposed to insert more than a row into the database.

$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>";
}
1
  • What is $emapData? What sort of data in it? Commented Apr 6, 2015 at 11:24

2 Answers 2

5

You could try using the MySQL built-in function for inserting CSV data, if the structure of your data is equal to your table structure it is pretty simple:

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

check MySQL LOAD DATA Documentation for more details

Sign up to request clarification or add additional context in comments.

Comments

3

Are you looking for something like this.

I didn't test is as I don't have sample data from you but please give it a go.

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

2 Comments

Yeah, This works well.. But please, what is the maximum number i can upload per a request. Thanks
That depends on your need. There are no maximum numbers apparently that is need to upload per a request. It will work well for your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.