Trying to piece together fgetcsv questions and answers to get where I need to be but do not grasp the basics enough to get the result I need. The Tab delimited file sample....
╔══════════════╦════════════╦═══════╦══════════╗
║ sku ║ asin ║ price ║ quantity ║
╠══════════════╬════════════╬═══════╬══════════╣
║ 00-1IAB-H5E9 ║ B008S0TEFQ ║ 5.00 ║ 1 ║
║ 00-BOXP-HDX4 ║ B00BP9N4JO ║ 20.00 ║ 1 ║
╚══════════════╩════════════╩═══════╩══════════╝
I need to update the database with a new quantity, based on the sku. Found a lot of similar answers, but not exactly what I need. Price and ASIN are ignored. Here is my code so far of the relevant portion.
$fin = fopen('qtyme.txt','r') or die('cant open file');
$link = mysql_connect('xxxx.com', 'xxxx', 'xxxx');
If (!$link) {
die ('Could not connect: ' . mysql_error());
}
@mysql_select_db('xxxx') or die ('Unable to select database');
echo "Connection succeeded, starting processing..... <br />\n";
while (($data=fgetcsv($fin,r,"/t"))!==FALSE) {
$query = "UPDATE products SET products_quantity = '".$data[3]."' WHERE products_sku = '".$data[0]."'";
mysql_query($query);
echo ($query) . "<br />\n";
}
fclose($fin);
mysql_close();
Obviously I do not know what I am doing, but it appears my issue is in defining the columns to pull out the proper data for the UPDATE.
help?
FINAL Solution: Thanks a lot ofr the help!
$link = mysql_connect('xxxx', 'xxxx', 'xxxx'); If (!$link) {
die ('Could not connect: ' . mysql_error()); } @mysql_select_db('xxxx') or die ('Unable to select database');
$sql = "TRUNCATE TABLE `tmpStock`"; mysql_query($sql); echo "Temporary Table Deleted...<br>";
$result = mysql_query("LOAD DATA LOCAL INFILE 'qtyme.txt' INTO TABLE tmpStock FIELDS TERMINATED BY '\t' ENCLOSED BY '\"' LINES TERMINATED BY '\n'")or die ('Error: '.mysql_error ()); echo "Temporary Data Uploaded and Inserted...<br>";
$result = mysql_query("UPDATE products a INNER JOIN tmpStock b ON a.products_sku = b.sku SET a.products_quantity = b.quantity")or die ('Error: '.mysql_error ()); echo "All Quantity's Updated...";
mysql_close($link);