PHP注意:未定义的偏移量

时间:2015-04-09 08:17:18

标签: 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
    }
}

由于

1 个答案:

答案 0 :(得分:1)

“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";