dongxizhe9755 2015-12-08 11:29
浏览 125
已采纳

使用PHP在MySQL中导入.xsl和.csv文件

I'm having an issue with CSV import to MySQL database:

<div style="border:1px dashed #333333; width:300px; margin:0 auto; padding:10px;">    
<form name="import" method="post" enctype="multipart/form-data">
    <input type="file" name="file" /><br />
    <input type="submit" name="submit" value="Submit" />
</form>
<?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)
    {
        $name = $filesop[0];
        $email = $filesop[1];           
        $sql = mysql_query("INSERT INTO selleruser (emailid, pass) VALUES ('$name','$email')");
        $c = $c + 1;
    }**     
        if($sql){
            echo "You database has imported successfully. You have inserted ". $c ." recoreds";
        }else{
            echo "Sorry! There is some problem.";
        }
   }?>

    </div>

I'm getting this error:

Undefined offset: 1 at thish line $email = $filesop1;

How can I import my .xsl file into the MySQL database. enter image description here

  • 写回答

1条回答 默认 最新

  • drsc10888 2015-12-09 13:05
    关注

    It looks like you have the issue with delimiter.

    fgetcsv($handle, 1000, ",")

    I can reproduce this issue if I save the file with delimiter ";". Thus, you may find with what delimiter file was saved and use it:

    <?php
    function detectDelimiter($csvFile)
    {
        $delimiters = array(
            ';' => 0,
            ',' => 0,
            "\t" => 0,
            "|" => 0
        );
    
        $handle = fopen($csvFile, "r");
        $firstLine = fgets($handle);
        fclose($handle); 
        foreach ($delimiters as $delimiter => &$count) {
            $count = count(str_getcsv($firstLine, $delimiter));
        }
    
        return array_search(max($delimiters), $delimiters);
    }
    #print_r(detectDelimiter('chart_column.csv'));
    
    
    $handle = fopen('chart_column.csv', "r");
    $delimiter = detectDelimiter('chart_column.csv');
    echo "I checked file and found that its delimiter is \"$delimiter\". Will use it further. 
    ";
    
    $c = 0;
    while(($filesop = fgetcsv($handle, 1000, $delimiter)) !== false)
        {
            $name = $filesop[0];
            $email = $filesop[1];           
            echo "emailid = $name , pass = $email
    ";
    #        $sql = mysql_query("INSERT INTO selleruser (emailid, pass) VALUES ('$name','$email')");
            $c = $c + 1;
        }
    

    As a result I've got:

    ~$ cat chart_column.csv 
    emailid;pass
    asd;asd
    fdga;ewrdf
    asdf;56gfh
    fgh5;fgh
    dfg;dfgdf
    dfg;dfgdf
    
    ~$ php delimiter.php
    I checked file and found that its delimiter is ";". Will use it further. 
    emailid = emailid , pass = pass
    emailid = asd , pass = asd
    emailid = fdga , pass = ewrdf
    emailid = asdf , pass = 56gfh
    emailid = fgh5 , pass = fgh
    emailid = dfg , pass = dfgdf
    emailid = dfg , pass = dfgdf
    

    Hope, this will help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录