Search code examples
phpcsvfgetcsv

Upload CSV file and check if value is empy to set it = 0 php


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++;
        }
    }
}

Solution

  • 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++;
            }
        }
    }