简体   繁体   English

将CSV文件导入MySQL数据库

[英]Importing a CSV file into MySQL database

I am trying to create a CSV importer for my website. 我正在尝试为我的网站创建CSV导入器。 I need to import some campaigns using a CSV file into my MySQL database. 我需要使用CSV文件将一些广告系列导入我的MySQL数据库。

The CSV file has 12 titles, but I only want to import the data from title 1, 3, 5, 6 and 8. The code below imports the csv file, but, it places the data for each title in the wrong places in the database. CSV文件有12个标题,但是我只想从标题1、3、5、6和8导入数据。下面的代码导入了csv文件,但是,每个标题的数据都放在了错误的位置。数据库。

Please could someone tell me where the error is? 请有人能告诉我错误在哪里吗?

Thanks - 谢谢 -

if ($_FILES[csv][size] > 0) { 

    //get the csv file 
    $file = $_FILES[csv][tmp_name]; 
    $handle = fopen($file,"r"); 

    //loop through the csv file and insert into database 
    do { 
        if ($data[0]) { 
            mysql_query("INSERT INTO campaigns (campaignname, url, requirements, rate, countrys) VALUES 
                ( 
                    '".addslashes($data[1])."', 
                    '".addslashes($data[3])."', 
                    '".addslashes($data[5])."', 
                    '".addslashes($data[6])."', 
                    '".addslashes($data[8])."' 
                ) 
            "); 
        } 
    } while ($data = fgetcsv($handle,0,",","'")); 
    // 

    //redirect 
    header('Location: import.php?success=1'); die; 

} 

我认为使用LOAD DATA INFILE比较简单

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

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