简体   繁体   English

上传CSV文件并导入mysql会出错

[英]Uploading a CSV file and importing to mysql giving an error

I am using the following script to import data into my mysql database from CSV files. 我使用以下脚本从CSV文件导入数据到我的mysql数据库。 The CSV is setup like this : CSV设置如下:

Name  Postcode
fred  hd435hg
bob   dh345fj

Above is what it looks like in excel, in raw csv format viewed in notepad it looks like this : 上面是它在excel中的样子,在记事本中查看的原始csv格式看起来像这样:

name,postcode
frank,ng435tj

The problem I am having is for some reason the postcode column isnt getting imported at all, also the header row is getting imported as a record too, is it possible to make it skip the first row ?. 我遇到的问题是由于某种原因邮政编码列根本没有导入,标题行也被导入作为记录,是否可以跳过第一行? I have been through the code and cant see why the postcode is not being pulled in, it is very odd. 我已经通过代码,无法看到为什么邮政编码没有被拉入,这是非常奇怪的。

        <?php
    //database connect info here

    //check for file upload
    if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){

        //upload directory
        $upload_dir = "./csv";

        //create file name
        $file_path = $upload_dir . $_FILES['csv_file']['name'];

        //move uploaded file to upload dir
        if (!move_uploaded_file($_FILES['csv_file']['tmp_name'], $file_path)) {

            //error moving upload file
            echo "Error moving file upload";

        }

        //open the csv file for reading
        $handle = fopen($file_path, 'r');


        while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {

            //Access field data in $data array ex.
            $name = $data[0];
            $postcode = $data[1];

            //Use data to insert into db
            $sql = sprintf("INSERT INTO test (name, postcode) VALUES ('%s',%d)",
                        mysql_real_escape_string($name),
                        $postcode
                        );
            mysql_query($sql) or (mysql_query("ROLLBACK") and die(mysql_error() . " - $sql"));
        }


        //delete csv file
        unlink($file_path);
    }
    ?>

Your CSV file seems to be a TSV file actually. 您的CSV文件实际上似乎是TSV文件。 It doesn't use commas, but tabulators for separating the fields. 它不使用逗号,而是使用制表符来分隔字段。

Therefore you need to change the fgetcsv call. 因此,您需要更改fgetcsv调用。 Instead of ',' use the tab: 而不是','使用标签:

 while (($data = fgetcsv($handle, 1000, "\t") ...

And to also skip the header row, add another faux fgetcsv before the while block: 并且还要跳过标题行,在while块之前添加另一个faux fgetcsv

 fgetcsv($handle);
 while (...) {

That will skip the first line. 那将跳过第一行。 (A simple fgets would also do.) (简单的fgets也可以。)


Oh, just noticed: The postcode might also get dropped because you concat it into the string as decimal with the sprintf placeholder %d for $postcode . 哦,刚刚注意到:邮政编码可能也会被删除,因为你将它连接成字符串为十进制, sprintf占位符%d$postcode Should that field actually contain lettery, like in your example, then that wouldn't work. 如果该字段实际上包含字母,就像在您的示例中那样,则不起作用。 -- Though I presume that's just a wrong example printout. - 虽然我认为打印输出只是一个错误的例子。

Try this one. 试试这个吧。

$file = $_FILES['file']['tmp_name'];

        $handle = fopen($file,"r");

        while(($fileop = fgetcsv($handle,1000,",")) !==false)
        {
            $username = $fileop[0];
            $name = $fileop[1];
            $address = $fileop[2];

            $sql = mysql_query("INSERT INTO ...... ");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM