Reputation: 187
I'm trying to import a csv file to my database. It's working but the problem is I have a column, namely "address1" and "address2" which may have commas. Example is:
194 Chavez compound, Something City
What's happening is194 Chavez compound is in the "address1" and Something City is in "address2". The whole address should be in "address1" only.
This is my code on importing in my database:
do {
if ($data[0]) {
mysql_query("INSERT INTO awb (recNAME, company, address1, address2, city, province, postalCODE, contactNO, email, commodity, type, weight, length, width, height, decVAL, specINSTRUC, shipREF, payMETHOD, packNO, trackNO) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."',
'".addslashes($data[3])."',
'".addslashes($data[4])."',
'".addslashes($data[5])."',
'".addslashes($data[6])."',
'".addslashes($data[7])."',
'".addslashes($data[8])."',
'".addslashes($data[9])."',
'".addslashes($data[10])."',
'".addslashes($data[11])."',
'".addslashes($data[12])."',
'".addslashes($data[13])."',
'".addslashes($data[14])."',
'".addslashes($data[15])."',
'".addslashes($data[16])."',
'".addslashes($data[17])."',
'".addslashes($data[18])."',
'".addslashes($data[19])."',
'".addslashes($data[20])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
Upvotes: 0
Views: 1551
Reputation: 1103
As one of the commentors suggested, try using LOAD DATA INFILE instead:
LOAD DATA INFILE '[file name]' INTO TABLE '[table name]'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
If your CSV file has a header row, you'll also want to add this clause: IGNORE 1 LINES.
Upvotes: 1