Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP import csv to mysql issue
    primarykey
    data
    text
    <p>I have a csv file with almost 8000 lines. I need to insert som e columns to the database. For that I am using the below code:</p> <pre><code>$csv_file = $_FILES[file][tmp_name]; $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['code'] = $csv_array[6]; $insert_csv['description'] = $csv_array[7]; $insert_csv['rrp'] =$csv_array[8]; $insert_csv['buy'] = $csv_array[9]; $insert_csv['stock'] = $csv_array[11]; if($insert_csv['code']!="" &amp;&amp; $insert_csv['description']!="" &amp;&amp; $insert_csv['rrp']!="" || $insert_csv['buy']!="" || $insert_csv['stock']!="") { $query = "INSERT INTO pricing (code, description,rrp,buy,stock,distributor,vendor,version) VALUES ('".$insert_csv['code']."','".$insert_csv['description']."','".$insert_csv['rrp']."','".$insert_csv['buy']."','".$insert_csv['stock']."', '".$d."','".$v."','".$version."')"; $n=mysql_query($query, $con); } $i++; } fclose($csvfile); header('Location: import.php?success=1'); die; </code></pre> <p>But the issues is, sometimes some columns itself has comma separated values. So the insertion of database is difficult using my code. So the insertion of values will be wrong column values.</p> <p>Eg:</p> <p>correct csv data:(This will insert correctly to the database)</p> <p><strong>2132,JUNIPER,CONTENT SUBSCRIPTIONS,First Year Subscriptions,A,GEN,SRX220-IDP,1 year license for IDP updates for SRX 220,951.3514,599.3514,37%,0,0,0,0,31/10/13 21:16,0,21</strong></p> <p>But sometimes the csv file contains the following line format:</p> <p>Here the $csv_array[2] itself has comma separated values but covered with quotes. My code is only looking for commas. SO the insertion values will be different in the db table.</p> <p><strong>2132,JUNIPER,"J, SRX, AX, VGW",Base System,A,GEN,AX411-KR,Dual Radio 802.11abgn Access Point for Korea. Power supply not included.,805.4054,507.4054,37.01%,0,0,0,0,31/10/13 21:16,0,21</strong></p> <p>How can I fix it?</p> <p>Thanks!</p> <p><strong>UPDATE</strong></p> <pre><code>if ($_FILES[file][size] &gt; 0) { //get the csv file $file = $_FILES[file][tmp_name]; $handle = fopen($file,"r"); fgetcsv($handle,10000,","); //loop through the csv file and insert into database do { if ($data[0]) { mysql_query("INSERT INTO pricing (code, description,rrp,buy,stock,distributor,vendor,version) VALUES ( '".addslashes($data[6])."', '".addslashes($data[7])."', '".addslashes($data[8])."', '".addslashes($data[9])."', '".addslashes($data[11])."', '".$d."', '".$v."', '".$version."' ) "); } } while ($data = fgetcsv($handle,1000,',','"')); header('Location: import.php?success=1'); die; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload