I have what I think is a CSV question. I'm having some issues with a massive update that, because it involves several inter-related tables, I can't just dump into MySql but instead must read through a huge csv file and insert the data one line at a time. Anyway, the problem I'm having is that the data is periodically inserted improperly and I'm at a loss to find a reason. I will get 50 or 100 good rows and then it spits the bit and inserts everything wrong, then, after a few bad inserts, picks back up correctly again. Here is the code for stepping through the csv once the file is open etc.
The insert id is then used to update other tables w/ the category and other info. Not my database design, so don't even get me started there... Anyway... as I said everything cruises along and then for some odd reason I will get an insert where everything but the zip code is put into a row, and then another row is created with no other info but the zipcode, or I'll get four consecutive rows where everything is fine, but the category from row one is carried down to rows two through four and then it corrects itself and moves on. I've checked the csv for any anomolies around the effected rows and there is nothing, no stray commas, apostrophes or anything like that, not even a common denominator such as all effected rows having no value for address or some such. I'm just wondering if either there is something that I'm missing here of if anyone else has encountered this type of issue before.
Code:
for ($c=0; $c < $number_of_fields; $c++)
{
$data_array[$header_array[$c]] = $data[$c];
}
$name = addslashes($data_array["Name"]);
$address = $data_array["Address"];
$phone = $data_array["Phone"];
$fax = $data_array["Fax"];
$url = $data_array["URL"];
$city = $data_array["City"];
$state = $data_array["State"];
$zip = $data_array["ZIP"];
$category = addslashes($data_array["Category"]);
$usn = "100".$current_row;
$psw = "900".$current_row;
$country = "United States";
$acl_user = 0;
$firstname = "Name";
$lastname = "Name";
$listing_start = date("Y-m-d");
$rank = 0;
$selected_rank = 0;
$status = "approved";
$expires = "2007-11-01";
$qa = "insert into firsttable
(organization, address, phone, fax, website, city, stateprov, postalcode, username, password, country, acl_user, firstname, lastname, customersince)
values
('$name', '$address', '$phone', '$fax', '$url', '$city', '$state', '$zip', '$usn', '$psw', '$country', '$acl_user', '$firstname', '$lastname', '$listing_start')";
The insert id is then used to update other tables w/ the category and other info. Not my database design, so don't even get me started there... Anyway... as I said everything cruises along and then for some odd reason I will get an insert where everything but the zip code is put into a row, and then another row is created with no other info but the zipcode, or I'll get four consecutive rows where everything is fine, but the category from row one is carried down to rows two through four and then it corrects itself and moves on. I've checked the csv for any anomolies around the effected rows and there is nothing, no stray commas, apostrophes or anything like that, not even a common denominator such as all effected rows having no value for address or some such. I'm just wondering if either there is something that I'm missing here of if anyone else has encountered this type of issue before.