-1

I want to upload a csv file in my database. If a cell of the csv file is empty i want to set the value = 0 because it gives error Undefined offset. i tried to check the values in a for loop but it does not works.

$msg = 0;
if (isset($_POST['import'])) {
    $fileName = $_FILES["file"]["tmp_name"];
    if ($_FILES["file"]["size"] > 0) {
        $file = fopen($fileName, "r");
        $i = 0;
        while (($column = fgetcsv($file)) !== FALSE) {
            if ($i > 0) {
                if (!empty($column[0])){
                    //$insertdate = date("Y-m-d", strtotime(str_replace('/', '-', $column[3])));
                    $sql = "INSERT into tab1 (country,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dece) 
                   values ('" . $column[0] . "','" . $column[1] . "','" . $column[2] . "','" . $column[3] . "','" . $column[4] . "','" . $column[5] . "','" . $column[6] . "','" . $column[7] . "','" . $column[8] . "','" . $column[9] . "','" . $column[10] . "','" . $column[11] . "','" . $column[12] . "')";
                    $result = mysqli_query($conn, $sql);
                    if (isset($result)) {
                        $msg++;
                    }
                }
            }
            $i++;
        }
    }
}
PX.
  • 5
  • 4
  • 1
    The problem it's you have to test the 13 elements exists if one does not you need to create it. – Inazo Nov 15 '19 at 12:46
  • It sounds to me like your CSV file is not consistent. It sounds like some rows do not have ALL 13 columns in them – RiggsFolly Nov 15 '19 at 12:49
  • how can i check if an elemen does not exist and create it? – PX. Nov 15 '19 at 12:53
  • Does this answer your question? [How to upload and parse a CSV file in php](https://stackoverflow.com/questions/5593473/how-to-upload-and-parse-a-csv-file-in-php) – pringi Nov 15 '19 at 13:04

1 Answers1

0

I'll update your code for add this section :

                if( count($column) < 13 ){

                    $tmpI = count($column);

                    while( $tmpI < 14 ){

                        $column[$tmpI] = 0;
                        $tmpI++;
                    }
                }  

The code check if you have 13 elements in your array, if not create the missing key with the value 0.

    $msg = 0;
if (isset($_POST['import'])) {
    $fileName = $_FILES["file"]["tmp_name"];
    if ($_FILES["file"]["size"] > 0) {
        $file = fopen($fileName, "r");
        $i = 0;
        while (($column = fgetcsv($file)) !== FALSE) {
            if ($i > 0) {
                if (!empty($column[0])){

                    if( count($column) < 13 ){

                        $tmpI = count($column);

                        while( $tmpI < 14 ){

                            $column[$tmpI] = 0;
                            $tmpI++;
                        }
                    }                   

                    //$insertdate = date("Y-m-d", strtotime(str_replace('/', '-', $column[3])));
                    $sql = "INSERT into tab1 (country,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dece) 
                   values ('" . $column[0] . "','" . $column[1] . "','" . $column[2] . "','" . $column[3] . "','" . $column[4] . "','" . $column[5] . "','" . $column[6] . "','" . $column[7] . "','" . $column[8] . "','" . $column[9] . "','" . $column[10] . "','" . $column[11] . "','" . $column[12] . "')";
                    $result = mysqli_query($conn, $sql);
                    if (isset($result)) {
                        $msg++;
                    }
                }
            }
            $i++;
        }
    }
}
Inazo
  • 488
  • 4
  • 15