Data from CSV to update SQL
Posted: Sun Dec 06, 2009 7:54 am
Hi to all,
I've one php script to update data in SQL. The script extract data from csv and update it in MYSQL database.
I need a small change on code. I want that php update data in sql if the quantity number is more than 3, if less put in SQL table 0 instead the real value.
Here is my code to update table from csv:
I want to update quantity with the right value if more than 3 and update quantity to 0 if less than 3
Hope you can help me....
Best Regards
Mozack
I've one php script to update data in SQL. The script extract data from csv and update it in MYSQL database.
I need a small change on code. I want that php update data in sql if the quantity number is more than 3, if less put in SQL table 0 instead the real value.
Here is my code to update table from csv:
Code: Select all
<?php
// Connect to MySQL
mysql_connect("DATABASE_ADDRESS", "DATABASE_USER", "DATABASE_PASS") or die(mysql_error());
mysql_select_db("DATABASE_TABLE") or die(mysql_error());
#if first row of csv file is headings set $row to 1.
$row = 1;
#database primary table
$table_to_update = "product_attribute";
#get the csv file
$handle = fopen("quantity.csv", "r");
#go through the csv file and print each row with fields to the screen.
#and import them into the database updating only the quantity
while (($data = fgetcsv($handle, 100000, ";")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
if ($c = 1) {
$ean = $data[($c - 1)];
echo $ean . " SKU Assigned <br />\n";
}
if ($c = 2) {
$quantity = $data[($c - 1)];
mysql_query("UPDATE $table_to_update SET quantity='$quantity' WHERE ean='$ean'")
or die(mysql_error());
echo $product_quantity . " Imported for row $row in product $ean13 <br />\n";
}
// would have to add an additional if statement for each field being updated and know the order of the fields from your csv file
//echo $data[$c] . "Imported <br />\n";
}
}
fclose($handle);
echo "<h1>Update Complete.</h1>";
?>Hope you can help me....
Best Regards
Mozack