dra8603 2012-06-12 12:34
浏览 49
已采纳

未定义的偏移:3错误excel文件上传和INSERT

I am using below code to upload excel and INSERT in mysql in php

public function read_file($table = 'users', $filename = 'test.xls') {


        $this->load->library('Spreadsheet_Excel_Reader');

        //$data = new Spreadsheet_Excel_Reader($pathToFile);
        $data = new Spreadsheet_Excel_Reader();
         $data->read($filename);

        $sql = "INSERT INTO $table (";

        for($index = 1;$index <= $data->sheets[0]['numCols']; $index++){
            $sql.= strtolower($data->sheets[0]['cells'][1][$index]) . ", ";
        }
        $valuesSQL = '';
        $sql = rtrim($sql, ", ")." ) VALUES ( ";
        for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {

            for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
                $valuesSQL .= "\"" . $data->sheets[0]['cells'][$i][$j] . "\"";
                if($j!=$data->sheets[0]['numCols']){
                        $valuesSQL .= ",";
                }
            }
            if($i!=$data->sheets[0]['numRows']){
                $valuesSQL .= "),(";
            }else{
                $valuesSQL .= ")";
            }
        }
        $sqlQuery = $sql . rtrim($valuesSQL);

        return($sqlQuery );
    }

I am getting below error

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 3

Filename: controllers/userInfo.php

Line Number: 154
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 3

Filename: controllers/userInfo.php

Line Number: 154

I am using codeigniter php framework.

line number 154 -> $valuesSQL .= "\"" . $data->sheets[0]['cells'][$i][$j] . "\"";

  • 写回答

1条回答 默认 最新

  • duanqiang9212 2012-06-12 13:26
    关注

    It means either that row 3 doesn't exist, or (more likely) that column "C" is an empty cell in one of the spreadsheet rows. You should test if isset($data->sheets[0]['cells'][$i][$j]) before using it, and set an appropriate NULL value in your SQL if it doesn't.

    if isset($data->sheets[0]['cells'][$i][$j]) {
        $valuesSQL .= "\"" . $data->sheets[0]['cells'][$i][$j] . "\""; 
    } else {
        $valuesSQL .= 'NULL'; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算