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.

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘