dpa31905 2013-10-07 07:42
浏览 30
已采纳

PHP CSVImport数据

I Created a file called import.php for PHP that I used to import a csv file. but its not working fine.i have 3 field in database.name id email email got alwyas numeric value 0 and name null.

Here is part of the script.

Any ideas why it would be being read as a long line?

   <?php

    $connect = mysql_connect('localhost','root','');
    if (!$connect) {
    die('Could not <span class="IL_AD" id="IL_AD3">connect to</span> <span class="IL_AD" id="IL_AD12">MySQL</span>: ' . mysql_error());
    }

    $cid =mysql_select_db('test',$connect);
    // supply your database name

    define('CSV_PATH','D:/xampp/htdocs/import/');
    // path where your CSV file is located

    $csv_file = CSV_PATH . "aa.csv"; // Name of your CSV file
    $csvfile = fopen($csv_file, 'r');
    $theData = fgets($csvfile);
    $i = 0; while (!feof($csvfile)) {
    $csv_data[] = fgets($csvfile, 1024);
    $csv_array = explode(",", $csv_data[$i]);
  $insert_csv = array();
    $insert_csv['ID'] = $csv_array[0];
    $insert_csv['name'] = $csv_array[1];
    $insert_csv['email'] = $csv_array[2];
    $query = "INSERT INTO csv(ID,name,email)
    VALUES('','".$insert_csv['name']."','".$insert_csv['email']."')";
    $n=mysql_query($query, $connect );
    $i++;
    }
    fclose($csvfile);    
    echo "File data successfully imported to database!!";
    mysql_close($connect);
    ?>




and this is my input file from where i upload csv file....i got Warning msg and my data base fiel id is created and email value i got 0 and name id null.



<html>
<body>

<form action="impt.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 
  • 写回答

1条回答 默认 最新

  • dqy13020940 2013-10-07 08:11
    关注

    This is your code using fgetcsv

    <?php
    
        $connect = mysql_connect('localhost','root','');
    
        if (!$connect) {
            die('Could not <span class="IL_AD" id="IL_AD3">connect to</span> <span class="IL_AD" id="IL_AD12">MySQL</span>: ' . mysql_error());
        }
    
        $cid = mysql_select_db('test', $connect);
        // supply your database name
    
        define('CSV_PATH','D:/xampp/htdocs/import/');
        // path where your CSV file is located
    
        $csv_file = CSV_PATH . "aa.csv"; // Name of your CSV file
        $csvfile = fopen($csv_file, 'r');
    
        $i = 0;
        while (($data = fgetcsv($csvfile, 1000)) !== FALSE) {
            if ($data != null) { // skip empty lines
                $insert_csv = array();
                $insert_csv['ID'] = $data[0];
                $insert_csv['name'] = $data[1];
                $insert_csv['email'] = $data[2];
                $query = "INSERT INTO csv(ID,name,email)
                          VALUES('','".$insert_csv['name']."','".$insert_csv['email']."')";
                $n=mysql_query($query, $connect );
            }
            $i++;
        }
    
        fclose($csvfile);    
        echo "File data successfully imported to database!!";
        mysql_close($connect);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀