0

I am trying to create a CSV importer for my website. I need to import some campaigns using a CSV file into my MySQL database.

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.

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; 

} 
  • can you print_r $data – Bhavin Rana Apr 12 '13 at 17:36
  • Read [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/1409082), do some debugging, and tell us where the error is. Then we can help you efficiently to solve it. – Jocelyn Apr 12 '13 at 17:36
  • are you sure you are using the right index? is title1 the first column of the array or the second ? – Miguelo Apr 12 '13 at 17:43
  • Please use `mysql_real_escape_string` instead of `addslashes` to escape – Steely Wing Apr 12 '13 at 18:12
  • http://stackoverflow.com/questions/20876043/php-script-to-import-csv-data-into-mysql – miralong Mar 01 '15 at 17:17
  • [http://stackoverflow.com/questions/20876043/php-script-to-import-csv-data-into-mysql][1] [1]: http://stackoverflow.com/questions/20876043/php-script-to-import-csv-data-into-mysql – miralong Mar 01 '15 at 17:18

1 Answers1

0

I think is simplier to use LOAD DATA INFILE

m4t1t0
  • 5,669
  • 3
  • 22
  • 30