导入.xlsx时php中的列名不能为null

时间:2018-12-13 09:47:07

标签: php codeigniter

当我选择上载.xlsx文件时,它会导入工作表中显示的所有数据,但最后它显示错误 错误号:1048 列“名称”不能为空 为什么数据库不为空类型时名称为空? 我确实选择了不同的excel工作表,但发生了相同的事情

这是我的控制器 Import_Controller.php

    class Import_Controller extends CI_Controller {
public function __construct() {
      parent::__construct(); 
      $this->load->model('Import_Model');
      $this->load->library('excel');

      if (!$this->ion_auth->logged_in())
      {
            // redirect them to the login page
        redirect('auth/login', 'refresh');
    }

}

public function index(){
    $data['content'] = 'admin/import/Import';
    $this->load->view('inc/template',$data);
}


public function import()
 {
  if(isset($_FILES["file"]["name"]))
  {
   $path = $_FILES["file"]["tmp_name"];
   $object = PHPExcel_IOFactory::load($path);
   foreach($object->getWorksheetIterator() as $worksheet)
   {
    $highestRow = $worksheet->getHighestRow();
    $highestColumn = $worksheet->getHighestColumn();

    for($row=2; $row<=$highestRow; $row++)
    {
        $name = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
        $size = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
        $manufacturer = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
        $model_no = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
        $model_name = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
        $description = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
        $width = $worksheet->getCellByColumnAndRow(6, $row)->getValue();
        $depth = $worksheet->getCellByColumnAndRow(7, $row)->getValue();
        $kit_pn = $worksheet->getCellByColumnAndRow(8, $row)->getValue();
        $color = $worksheet->getCellByColumnAndRow(9, $row)->getValue();
        $remarks = $worksheet->getCellByColumnAndRow(10, $row)->getValue();  
        $arr['dm'] = array('name'  => $name,'size'   => $size);
        $arr['im'] = array('manufacturer' => $manufacturer,'model_no' => $model_no,'model_name' => $model_name,'description' => $description,'width' => $width,'depth' => $depth);
        $arr['ak'] = array('kit_pn' => $kit_pn,'color' => $color,'remarks' => $remarks);

        $new_array[] = $arr;    
    }
   }        

   $this->Import_Model->insert($new_array);

      echo 'Data Imported successfully';

  } 
 }
}

我的模型是: Import_model.php

    class Import_Model extends CI_Model
{
    public function getValues($value){

        $data = $this->db->get_where('dispenser_model', $value['dm'])->row();


        if(empty($data)){

            $this->db->insert('dispenser_model',$value['dm']);
            //$dm_insert_id = $this->db->insert_id();

        }  else{

            $dm_insert_id = $data->id;
        }


        $data1 = $this->db->get_where('im_manufacturer', $value['im'])->row();

        if(empty($data1)){
            $this->db->insert('im_manufacturer',array(
                'manufacturer' =>$value['im']['manufacturer'],
                'model_no' => $value['im']['model_no'],  
                'model_name' => $value['im']['model_name'],
                'description' => empty($value['im']['description']) ? $value['im']['description'] : NULL,
                'width' => $value['im']['width'],
                'depth' => $value['im']['depth']
            ));

            //echo $this->db->last_query();

            $im_insert_id = $this->db->insert_id();

        }   else{

            $im_insert_id = $data1->id;
        }


        $data2 = $this->db->get_where('adaptor_kit',$value['ak'])->row();

        if(empty($data2)){
            $this->db->insert('adaptor_kit',$value['ak']);

            $ak_insert_id = $this->db->insert_id();

        }   else{
            $ak_insert_id = $data2->id;
        } 

        $data3 = $this->db->get_where('model', array(
            'dm_id' => $dm_insert_id,
            'im_id' =>$im_insert_id,
            'ak_id'=> $ak_insert_id))->row();

        if(empty($data3)){
            $this->db->insert('model',array(
                'dm_id' => $dm_insert_id,
                'im_id' =>$im_insert_id,
                'ak_id'=> $ak_insert_id));

            $model_insert_id = $this->db->insert_id();

        }   else{
            $model_insert_id = $data->id;
        } 


    }

    public function insert($new_array)
    {
        $my_array = [];

        foreach ($new_array as $value) {

        $my_array[] = $this->getValues($value);

        echo '<pre>' ; print_r($value);

        }

    }

}

查看文件: Import.php

    <div class="breadcrumbs">
  <div class="breadcrumbs-inner">
    <div class="row m-0">
      <div class="col-sm-4">
        <div class="page-header float-left">
          <div class="page-title">
            <h1><?php echo lang('dashboard_heading');?></h1>
          </div>
        </div>
      </div>
      <div class="col-sm-8">
        <div class="page-header float-right">
          <div class="page-title">
            <ol class="breadcrumb text-right">
              <li><a href="<?php echo base_url('index.php/admin/Dashboard_Controller') ?>"><?php echo lang('dashboard_heading');?></a></li>
              <li><a href="#">Import data</a></li>
            </ol>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="content">
  <div class="animated fadeIn">
    <div class="row">
      <div class="col-lg-12">           
        <h4>Import Data   
        <div class="pull-right">  
            <a class="btn btn-primary" href="<?php echo base_url('assets/spreadsheet/sample.xlsx')?>">Download template <i class="menu-icon fa fa-download"></i></a>
        </div>
        </h4>
      </div>
      <br><br><br>
      <div class="col-lg-12"> 
        <h4>Upload File </h4>   
        <br>   

        <form method="post" id="import_form" enctype="multipart/form-data">
            <input type="file" name="file" id="file" required accept=".xls, .xlsx" class="input-large" >
            <button type="submit" id="submit" name="import" class="btn btn-primary button-loading">Import</button>
        </form>
        <br />
        <div id="customer_data">
        </div>
    </div>
    </div>
  </div>
</div>

<script>
$(document).ready(function(){

 load_data();

 function load_data()
 {
    $('#import_form').on('submit', function(event){
  event.preventDefault();
  $.ajax({
   url:"<?php echo base_url(); ?>index.php/admin/Import_Controller/import",
   method:"POST",
   data:new FormData(this),
   contentType:false,
   cache:false,
   processData:false,
   success:function(data){
    $('#file').val('');
    load_data();
    alert(data);
   }
  })
 });

 }



});
</script>

0 个答案:

没有答案