PHP通知: Undefined offset

标签 php mysql excel

我想将csv数据上传到mysql,不包括标题。我的脚本可以工作,但显示错误

Notice: Undefined offset: 32 in C:\wamp\www\mvc\excel\upload.php on line 38

执行查询后的消息。出了什么问题以及如何修复它。

这是我的代码

if (isset($_POST['submit'])) {


    //Import uploaded file to Database

    $file = $_FILES['filename']['tmp_name']; 

    $handle = fopen($file,"r"); 

    //loop through the csv file and insert into database 
    for ($lines = 0; $data = fgetcsv($handle,1000,",",'"'); $lines++) {
        if ($lines == 0) continue;
        if ($data[0]) {

            $import="INSERT into contacts 
                  (name, contacttype, nationality, mobile1, country1,
                  email1, phone, fax, twon, area, restatus, retype, 
                  reproject1, contactgroup, freezone1, gender, 
                  dateofbirth, married, children, educationlevel, 
                  jobsector, driverslicense, media, media2, age, 
                  salary, created, upload, image, companyname, 
                  businesscategory, subcategory, jobrole, upload_id)
                  values('$data[0]', '$data[1]','$data[2]','$data[3]',
                         '$data[4]', $data[5]','$data[6]','$data[7]',
                         '$data[8]','$data[9]','$data[10]','$data[11]',
                         '$data[12]','$data[13]','$data[14]','$data[15]',
                         '$data[16]','$data[17]','$data[18]','$data[19]',
                         '$data[20]','$data[21]','$data[22]','$data[23]',
                         '$data[24]','$data[25]','$data[26]','$data[27]',
                         '$data[28]','$data[29]','$data[30]','$data[31]',
                         '$data[32]','$uploadid') ";
            print $import;
            mysql_query($import) or die(mysql_error());
        }

        fclose($handle);

        print "Import done";

        //view upload form
    }
}

谢谢

最佳答案

“ undefined offset ”意味着代码引用了缺失的数组索引。 为了防止这种情况,你应该这样使用:

for ($lines = 0; $data = fgetcsv($handle,1000,",",'"'); $lines++) {
        if ($lines == 0) continue;
        for ($i = 0; $i <= 32; $i ++)
        {
            if (!isset($data[$i]))
                $data[$i] = ''; 
        }

                $import="INSERT into contacts 
                  (name, contacttype, nationality, mobile1, country1,
                  email1, phone, fax, twon, area, restatus, retype, 
                  reproject1, contactgroup, freezone1, gender, 
                  dateofbirth, married, children, educationlevel, 
                  jobsector, driverslicense, media, media2, age, 
                  salary, created, upload, image, companyname, 
                  businesscategory, subcategory, jobrole, upload_id)
                  values('$data[0]', '$data[1]','$data[2]','$data[3]',
                         '$data[4]', $data[5]','$data[6]','$data[7]',
                         '$data[8]','$data[9]','$data[10]','$data[11]',
                         '$data[12]','$data[13]','$data[14]','$data[15]',
                         '$data[16]','$data[17]','$data[18]','$data[19]',
                         '$data[20]','$data[21]','$data[22]','$data[23]',
                         '$data[24]','$data[25]','$data[26]','$data[27]',
                         '$data[28]','$data[29]','$data[30]','$data[31]',
                         '$data[32]','$uploadid') ";
                print $import;
                mysql_query($import) or die(mysql_error());
}
fclose($handle);
print "Import done";

关于PHP通知: Undefined offset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29533309/

相关文章:

javascript - 如何仅在条件为真时创建 HTML

php - 提取多维数组一部分的有效方法

javascript - 拖放图像输入文件并在上传前预览

mysql - 如何在grails executeupdate中使用mysql时间函数

excel - 如何计算Excel中的连续值?

vba - 严格使用 Excel 打开 .xlsm

php - 如何在 Laravel 5.6 json 响应中将空值转换为空字符串?

php - Laravel 迁移

php - 使用 php 和 MySql 保存可排序列表的顺序

excel - 循环遍历指定范围内的单元格