首页 > 解决方案 > PHP为空col导入csv NULL

问题描述

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

您好,我正在使用此脚本加载 csv 文件,它可以完美运行,除非数字字段未填充。如何为文件的未填充字段输入 NULL 值?

标签: phpfgetcsv

解决方案


如果我错了,请纠正我,但我认为空列在读取 csv 文件时应该返回一个空字符串,在这种情况下,您可以使用 PHP 中的方法,例如 isset 或 empty,如果您想分配空值,只需使用一个班轮 if 语句如

如果 $getData[0] 为空则返回 null 否则返回 $getData[0]

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

或者

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

推荐阅读