【问题标题】:Upload CSV file and check if value is empy to set it = 0 php上传 CSV 文件并检查值是否为空以将其设置为 = 0 php
【发布时间】:2020-03-11 15:00:27
【问题描述】:

我想在我的数据库中上传一个 csv 文件。如果 csv 文件的单元格为空,我想将值设置为 0,因为它会给出错误未定义的偏移量。我试图检查 for 循环中的值,但它不起作用。

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

【问题讨论】:

  • 问题是你必须测试这 13 个元素,如果你不需要创建它。
  • 听起来您的 CSV 文件不一致。听起来有些行中没有全部 13 列
  • 如何检查元素是否不存在并创建它?
  • 这能回答你的问题吗? How to upload and parse a CSV file in php

标签: php csv fgetcsv


【解决方案1】:

我将更新您的代码以添加此部分:

                if( count($column) < 13 ){

                    $tmpI = count($column);

                    while( $tmpI < 14 ){

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

代码检查数组中是否有 13 个元素,如果没有,则创建值为 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++;
        }
    }
}

【讨论】:

  • 我做了一个更新,你可以检查一下
猜你喜欢
  • 2019-06-14
  • 1970-01-01
  • 2011-10-03
  • 2022-11-12
  • 2021-12-25
  • 1970-01-01
  • 2011-02-25
  • 2021-09-11
  • 1970-01-01
相关资源
最近更新 更多