Forum Moderators: coopster

Message Too Old, No Replies

Insert from CSV file

         

Pico_Train

12:35 pm on Oct 14, 2009 (gmt 0)

10+ Year Member



Hi there, I am trying to insert a csv file into my db but it inserts more rows than are in the CSV file, inserting duplicate rows. Here is my code:

$row = 1;
$handle = fopen ("HotelsLive.csv","r");
while (($data = fgetcsv ($handle,",")) !== FALSE)
{
$sql = 'INSERT into hotel(hotel_code,hotel, city_code, country_code, address, postcode,tel, fax,email,
starcode, boardbasis, category, rooms, hotel_area, public_desc, tube, bedroom_pic,front_pic,
map_pic, IsEnabled)
VALUES
("'.$data[0].'", "'.str_replace('"',"'",$data[1]).'", "'.$data[2].'","'.$data[3].'","'.$data[4].'"
,"'.$data[5].'","'.$data[6].'","'.$data[7].'","'.$data[8].'",'.$data[9].',"'.$data[10].'","'.$data[11].'"
,'.$data[12].',"'.str_replace('"',"'",$data[13]).'","'.str_replace('"',"'",$data[14]).'","'.str_replace('"',"'",$data[15]).'","'.$data[16].'","'.$data[17].'","'.$data[18].'","'.$data[19].'")';

echo $sql."<BR><BR>";

$add = mysql_query($sql) or die("Invalid query: " .
mysql_error().__LINE__.__FILE__);
$row++;

}
fclose($handle);

echo "All done!";

Now my question is, why is inserting duplicate rows? I know there are 8359 rows in this csv file but why does it carry on and insert 9400 odd everytime?

Thanks for your help!

sned

4:35 pm on Oct 14, 2009 (gmt 0)

10+ Year Member



Are the extra rows inserted actual duplicates of earlier rows? Or are they just rows with no data?

I had a problem earlier with a csv upload -- excel deleted the values from the row, but didn't actually "delete" the row, so my upload code saw that there were a bunch of empty rows, and so inserted the empty data ...

-sned

Pico_Train

5:40 pm on Oct 14, 2009 (gmt 0)

10+ Year Member



No they were full on duplicates, I just rather checked for a value and if it already existed, skipped it, so problem solved!

Thanks though...