I'm banging my head trying to figure this out:
I'm trying to update a price in table Our_Price from a csv file by comparing it with a second table Products, but its not updating the table.
csv data is:
num, sku,list_price, our_price
"1","12345","60.98","56.98"
"2","12346","70.98","66.98"
"3","12347","90.98","86.98"
...
The "sku" value in the csv file is what needs to be compared to the "productcode" in the table Products.
The Our_price and Products tables have a common field "productid"
Products table has columns
productid productcode list_price etc.
Our Price table has columns
productid Our_price etc.
Here's my code:
<?php
mysql_pconnect("localhost", "username", "password") or die("Could not connect : " . mysql_error());
mysql_select_db("dbName");
$handle = fopen("price_update.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$sku = (int) $data[1];
echo "sku = ".$sku."\n\n";
$list_price = floatval($data[2]);
echo "List Price = ".$list_price."\n\n";
$our_price = floatval($data[3]);
echo "Our Price = ".$our_price."\n\n";
$query = 'SELECT p.productcode FROM Products p JOIN Our_Price pp ON pp.productid=p.productid';
if (!$result = mysql_query($query)) {
continue;
}
if ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$query = "UPDATE Our_Price pp SET pp.list_price='$list_price' WHERE p.productcode=$sku";
mysql_query($query);
if (mysql_affected_rows() <= 0) {
echo "no rows affected\n\n";
}
} else {
}
mysql_free_result($result);
}
fclose($handle);
?>
Any help would be greatly appreciated.
Thanks.