Lanz Joshua
Lanz Joshua

Reputation: 51

How to insert multiple csv into different tables depending on the filename?

Here is the snippet of my code in which I want to insert the data inside in the csv depending on the filename of the csv file.

For example, my selected files are: ICSLAB1.csv, ICSLAB2.csv, ICSLAB3.csv, ICSLAB4.csv, each csv should be inserted into their respective db tables which are room_icslab1, room_icslab2, room_icslab3, room_icslab4 respectively.

In this code, all of the csv files are inserting only in the db table room_icslab1.

if(isset($_POST['roomschedule'])){

$tmpfile = $_FILES["file"]["tmp_name"];

$fname = $_FILES["file"]["name"]['0'];
if($fname == "ICSLAB1.csv"){
    $tableName = "room_icslab1";
}elseif($fname == "ICSLAB2.csv"){
    $tableName = "room_icslab2";
}elseif($fname == "ICSLAB3.csv"){
    $tableName = "room_icslab3";
}elseif($fname == "ICSLAB4.csv"){
    $tableName = "room_icslab4";
}

foreach ($tmpfile as $key => $filename) {

    if ($_FILES["file"]["size"] > 0) {
        $file = fopen($filename, "r");
        $count = 0;

        while (($uploadData = fgetcsv($file, 1000, ",")) !== FALSE) {


            $count++;

            $day = $uploadData[0];
            $startTime = $uploadData[1];
            $endTime = $uploadData[2];
            $room = $uploadData[3];
            $subject = $uploadData[4];
            $section = $uploadData[5];
            $firstname = $uploadData[6];
            $lastname = $uploadData[7];

            if ($count > 1) {

                $sql = "INSERT into ".$tableName."(day, startTime, endTime, room, subject, section, firstname, lastname, fullname,filename)
                values ('$day',time(str_to_date('".$startTime."', '%h:%i %p')),time(str_to_date('".$endTime."', '%h:%i %p')), '$room', '$subject', '$section', '$firstname', '$lastname', '$firstname $lastname', '$fname')";

                mysqli_query($conn,$sql) or die(mysqli_error());
            }
        }

        fclose($file);

Upvotes: 0

Views: 53

Answers (1)

Don't Panic
Don't Panic

Reputation: 41820

Looks like you need to move your name if/elseif block that sets $tablename inside your loop, so that it will check the name of each file. The way it currently is, before the loop

$fname = $_FILES["file"]["name"]['0'];

will only refer to the first one, set $tablename to that one, and use that same value throughout the loop.

Use [$key] instead of [0] to refer to the file in the current iteration of the loop. Here's an example of that part of the code

// ...
foreach ($tmpfile as $key => $filename) {

    $fname = $_FILES["file"]["name"][$key];
    //                                ^^ Index of the current iteration      
    if($fname == "ICSLAB1.csv"){
        $tableName = "room_icslab1";
    } elseif($fname == "ICSLAB2.csv"){
        $tableName = "room_icslab2";
    } elseif($fname == "ICSLAB3.csv"){
        $tableName = "room_icslab3";
    } elseif($fname == "ICSLAB4.csv"){
        $tableName = "room_icslab4";
    }
    // ...

You can check the PHP documentation for more details. In particular, look at "Example #3 Uploading array of files".

Upvotes: 1

Related Questions