fgetcsv - PHP import csv NULL for empty col

function  upload_excel1 (){
    global $conn; 
    $filename=$_FILES["file"]["tmp_name"];      

    if($_FILES["file"]["size"] > 0)
    {
       ini_set("auto_detect_line_endings", true);
       $file = fopen($filename, "r");
       $flag = true;
       $row = 1;

       while (($getData = fgetcsv($file, 10000, ";")) !== FALSE)
        {
           if($flag) { $flag = false; continue; }

           $sql = "INSERT into analisi (id, id_profili, id_orizzonte, rap_prova,campione,scheletro,sabbia, limo, argilla, tessitura, reazione, ec12, calcare_tot, calcare_att, sostanza_org, azoto_tot, fosforo_p, calcio_mg, magnesio_mg, potassio_mg, sodio_mg, csc_meq, calcio_meq, magnesio_meq, potassio_meq, sodio_meq, saturazione_bas, rapporto_mgk, rapporto_cak, rapporto_camg) 
           values (DEFAULT,'".pg_escape_string($getData[0])."','".pg_escape_string(($getData[1]))."','".pg_escape_string(($getData[2]))."','".pg_escape_string(($getData[3]))."','".pg_escape_string(($getData[4]))."','".pg_escape_string(($getData[5]))."','".pg_escape_string(($getData[6]))."','".pg_escape_string(($getData[7]))."','".pg_escape_string(($getData[8]))."','".pg_escape_string(($getData[9]))."','".pg_escape_string(($getData[10]))."','".pg_escape_string(($getData[11]))."','".pg_escape_string(($getData[12]))."','".pg_escape_string(($getData[13]))."','".pg_escape_string(($getData[14]))."','".pg_escape_string(($getData[15]))."','".pg_escape_string(($getData[16]))."','".pg_escape_string(($getData[17]))."','".pg_escape_string(($getData[18]))."','".pg_escape_string(($getData[19]))."','".pg_escape_string(($getData[20]))."','".pg_escape_string(($getData[21]))."','".pg_escape_string(($getData[22]))."','".pg_escape_string(($getData[23]))."','".pg_escape_string(($getData[24]))."','".pg_escape_string(($getData[25]))."','".pg_escape_string(($getData[26]))."','".pg_escape_string(($getData[27]))."','".pg_escape_string(($getData[28]))."')";
           $stmt = $conn->prepare($sql);
           $stmt->execute();
       }
           if(!isset($stmt))
           {
               echo "<script type=\"text/javascript\">
                       alert(\"Invalid File:Please Upload CSV File.\");
                       window.location = \"index.php\"
                     </script>";   
           }
           else {
               echo "<script type=\"text/javascript\">
                   alert(\"CSV File has been successfully Imported.\");
                   window.location = \"index.php\"
               </script>"; 
           }

        
        fclose($file); 
        ini_set("auto_detect_line_endings", false);
    }
     }

Hello i am using this script to load a csv file, it works perfectly, except in case the numeric fields turn out not filled. How can I enter a NULL value for the unfilled fields of the file?

Answer

Solution:

Now it's work :)

if(!empty($getData[15])) { $getData[28]= "'$getData[28]'"; }else{ $getData[28]= 'NULL';}
               
   
               $sql = "INSERT into analisi (id, id_profili, id_orizzonte, rap_prova,campione,scheletro,sabbia, limo, argilla, tessitura, reazione, ec12, calcare_tot, calcare_att, sostanza_org, azoto_tot, fosforo_p, calcio_mg, magnesio_mg, potassio_mg, sodio_mg, csc_meq, calcio_meq, magnesio_meq, potassio_meq, sodio_meq, saturazione_bas, rapporto_mgk, rapporto_cak, rapporto_camg) 
               values (DEFAULT,$getData[0],$getData[1],$getData[2],$getData[3],$getData[4],$getData[5],$getData[6],$getData[7],$getData[8],$getData[9],$getData[10],$getData[11],$getData[12],$getData[13],$getData[14],$getData[15],$getData[16],$getData[17],$getData[18],$getData[19],$getData[20],$getData[21],$getData[22],$getData[23],$getData[24],$getData[25],$getData[26],$getData[27],$getData[28])";
         

Answer

Solution:

Correct me if I'm wrong, but I think empty column should return an empty string when reading a csv file, in that case you can use method in PHP like isset or empty and if you want to assign the null value just use a one liner if statement like

if $getData[0] is empty then return null else return $getData[0]

empty($getData[0]) ? null : pg_escape_string($getData[0])

or

isset($getData[0]) ? null : pg_escape_string($getData[0])

Source