dongle19863 2014-10-21 03:46
浏览 32

使用php在mysql中加载csv文件时出错

Could some one help me out for some reason im getting an error and can not figure out why!!!! I have about 45 columns and im only needing to use 1,2,3,4,7,8 from the table so i cant load everything.

    <? 
        $con=mysqli_connect("localhost","*******","*****","******");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
        //get the csv file
    $file = "tickets/feed/rtc.csv";
    $handle = fopen($file,"r");
    $count=0;
    $sid="32837459823";
    //loop through the csv file and insert into database
    do {
        if ($data[0]) {
            mysqli_query($con,"INSERT INTO carlist (id, stknum, vin, vt, stat, other, store_id) VALUES
                (
                    '".$count++."',
                    '".addslashes($data[7])."',
                    '".addslashes($data[8])."',
                    '".addslashes($data[2].$data[3].$data[4])."',
                    '".addslashes($data[1])."',
                    '".addslashes("0")."',
                    '".addslashes($sid)."'
                )
            ");
        }
    } while ($data = fgetcsv($handle,1000,",","'"));
    //
        ?>

Here is the errors im getting

Notice: Undefined variable: data in /home1/inventory/mobile/actions/loadsubmit.php on line 139

Notice: Undefined offset: 7 in /home1/inventory/mobile/actions/loadsubmit.php on line 139

Notice: Undefined offset: 8 in /home1/inventory/mobile/actions/loadsubmit.php on line 140

Notice: Undefined offset: 2 in /home1/inventory/mobile/actions/loadsubmit.php on line 141

Notice: Undefined offset: 3 in /home1/inventory/mobile/actions/loadsubmit.php on line 141

Notice: Undefined offset: 4 in /home1/inventory/mobile/actions/loadsubmit.php on line 141

Notice: Undefined offset: 1 in /home1/inventory/mobile/actions/loadsubmit.php on line 142

  • 写回答

1条回答 默认 最新

  • drd0833 2014-10-21 03:50
    关注

    Looks like $data is not defined until you hit

    while ($data = fgetcsv($handle,1000,",","'"));
    

    so when you're trying to access $data before that line you're getting an error because the variable doesn't exist.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题