首页 > 解决方案 > 尝试访问 PhpSpreadsheet 中 null 类型值的数组偏移量

问题描述

我正在使用 ajax 上传 excel 表并将数据插入数据库。我测试过,它正在工作。

但是我在网络选项卡中遇到了一个错误。

注意:尝试访问第 1934 行 /opt/lampp/htdocs/test/assets/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php 中 null 类型值的数组偏移量

我在 excel 工作表中只有 1 条记录,当我导入工作表时,我得到三行(第一行是正确的,其余两行是 NULL)。

我正在使用下面的代码

HTML

<form method="post" id="import_excelsheet" name="import_excelsheet" enctype="multipart/form-data">
 <div class="form-group"><input type="file" name="list21" /></div>
 <input type="hidden" name="action" value="list21Import">
 <input type="submit" name="listImport21" id="list_import" class="btn btn-primary" value="Import list" />
</form>

JS

$('#import_excelsheet').validate({
    rules: {
        list21: {
            required: true,
            extension: "xlsx|xls|xlsm|csv"
        }
    },
    messages: {
        list21: {
            required: "file .xlsx, .xlsm, .xls, .csv only.",
            extension: "Please upload valid file formats .xlsx, .xlsm, .xls, .csv only."
        }
    },

submitHandler: function(form) {  
      $.ajax({
      url:"process.php",
      method:"POST",
      data:new FormData($('#import_excelsheet')[0]),
      contentType:false,
      cache:false,
      processData:false,
      success:function(data)
      {
        alert("success");
        //location.reload();
      }
    }) ;    
         }
});

Process.php(这只是一个函数)

function list21Import($pdo){
if($_FILES["list21"]["name"] != '')
{
         $allowed_extension = array('xls', 'csv', 'xlsx');
         $file_array = explode(".", $_FILES["list21"]["name"]);
         $file_extension = end($file_array);

         if(in_array($file_extension, $allowed_extension))
         {
          $file_name = time() . '.' . $file_extension;
          move_uploaded_file($_FILES['list21']['tmp_name'], $file_name);
          $file_type = \PhpOffice\PhpSpreadsheet\IOFactory::identify($file_name);
          $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($file_type);

          $spreadsheet = $reader->load($file_name);

          unlink($file_name);

          $data = $spreadsheet->getActiveSheet()->toArray();
          //print_r($data);
         
          $isheader = 0;
          foreach($data as $row)
          {

            $social['fb'] = trim($row[5]);
            $social['twitter'] = trim($row[6]);
            $social['linkedin'] = trim($row[7]);
            $social['youtube'] = trim($row[8]);
            $social['instagram'] = trim($row[9]);
            $social_links = serialize($social);

          if($isheader > 0) {
           $insert_data = array(
            ':name'  => $row[0],
            ':email'  => $row[1],
            ':content'  => $row[2],
            ':website'  => $row[3],
            ':logo'  => $row[4],
            ':social_links'  =>$social_links,
           );

           $query = "INSERT INTO `list21`(`name`, `email`, `content`, `website`, `logo`, `social_links`) VALUES (:name,:email,:content,:website,:logo,:social_links)";
           $statement = $pdo->prepare($query);
           $statement->execute($insert_data);

           } 
           else { 
            $isheader = 1; 
           }

          } // foreach
          $message = '<div class="alert alert-success">Data Imported Successfully</div>';

         } //in_array

         else
         {
          $message = '<div class="alert alert-danger">Only .xls .csv or .xlsx file allowed</div>';
         }

}
else
{
 $message = '<div class="alert alert-danger">Please Select File</div>';
}

echo $message;

}

你能帮我解决这个问题吗?

标签: phphtmljqueryajax

解决方案


推荐阅读