MaryaVodka
MaryaVodka

Reputation: 9

Import data from a txt file Database using Php

I have a problem with my Import File in PHP. I Have this code:

if(isset($_POST["Import"])){


    $filename=$_FILES["file"]["tmp_name"];
    if($_FILES["file"]["size"] > 0)
    {
        $file = fopen($filename, "r");
        $data = fgetcsv($file, 0, '|');

        while (($data = fgetcsv($file, 10000, "|")) !== FALSE)
        {
            $sql = "INSERT INTO
                    uanf_ver
                    (
                    r,
                    c,
                    l,
                    cr,
                    pr,
                    es,
                    ro,
                    bo,
                    cc,
                    sc,
                    lp,
                    cl,
                    sl
                    ) 
                   values (
                   '".addslashes($data[0])."',
                   '".addslashes($data[1])."',
                   '".addslashes($data[2])."',
                   '".addslashes($data[3])."',
                   '".addslashes($data[4])."',
                   '".addslashes($data[5])."',
                   '".addslashes($data[6])."',
                   '".addslashes($data[7])."',
                   '".addslashes($data[8])."',
                   '".addslashes($data[9])."',
                   '".addslashes($data[10])."',
                   '".addslashes($data[11])."',
                   '".addslashes($data[12])."'
                   )";
            $result = mysqli_query($conn, $sql);
        }

        fclose($file);
    }
}

The problem is when i load file with 1 line is not read. If i upload 3 lines the last line is not read and is not insert in database. Have any idea how can i fix this?

Upvotes: 0

Views: 90

Answers (1)

MaryaVodka
MaryaVodka

Reputation: 9

I resolved the problem.

        $data = fgetcsv($file, 0, '|');

this part need to be deleted and need to remain on the while

Upvotes: 1

Related Questions