dpp78272 2016-02-28 18:08
浏览 67

使用PHP将Excel / CSV导入MySQL的未定义偏移量

Im trying to import a .CVS file with First name and last name. But what it only does is import it to just one column in my database. And i get Notice: Undefined offset: 1 in ..insertimportres.php on line 91 and that is $enamn = $filesop[1]; that is complains on.

SQL enter image description here

CVS/Excel

enter image description here

PHP

<?php
    include ("connection.php");
        
    if(isset($_POST["submit"]))
{
    $file = $_FILES['file']['tmp_name'];
    $handle = fopen($file, "r");
    $c = 0;
    while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
    {
        $fnamn = $filesop[0];
        $enamn = $filesop[1];
    
        
        $sql = mysql_query("INSERT INTO kandidater (fnamn, enamn) VALUES ('$fnamn','$enamn')");
        $c = $c + 1;
    }
    
        if($sql){
            echo "You database has imported successfully";
        }else{
            echo "Sorry! There is some problem.";
        }
}

</div>
  • 写回答

1条回答 默认 最新

  • dongqi6964 2016-02-28 19:08
    关注

    Basically the error means that it's trying to get the value at index 1 but the index doesn't exist. It means that somewhere in your csv there is no value for the second column. To prevent such errors assign the variables like this:

     $fnamn = isset($filesop[0]) ? $filesop[0] : null;
     $enamn = isset($filesop[1]) ? $filesop[1] : null;
    

    If the index is set then it uses the value from that, if not it sets the value to null. Depending on your DB col definitions (if you have not null) this might fail also. In either case you can change null to whatever you need

    评论

报告相同问题?

悬赏问题

  • ¥15 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码