Xine Xine - 6 months ago 11
SQL Question

CSV to MySQL using PHP -- change value

I have a company that delivers there product feed with all details about stock, price etc as a CSV file. I use the following script to update my database with that CSV file:

<?php
echo "Update wordt uitgevoerd.... <br><br>";

$mysqlHost = "localhost";
$user = "username";
$password = "password";

$link = mysql_connect($mysqlHost, $user, $password)
or die('Could not connect: ' . mysql_error());
$handle = fopen("update.csv", "r");

while (($data = fgetcsv($handle, 1000, ";")) !== false) {
$model = mysql_real_escape_string ($data[0]);
$price = mysql_real_escape_string ($data[5]);
$quantity = mysql_real_escape_string ($data[7]);
mysql_select_db("wowtoxco_oc1", $link);

$result = mysql_query(
"UPDATE oc_product SET price='$price', quantity='$quantity'
WHERE model='$model'"
) or die(mysql_error());
echo $model . " - art.nr. locked -> ";
echo $price . " prijs updated -> ";
echo $quantity . " voorraad updated.<br>";
}

fclose($handle);
mysql_close($link);

echo "<br>Script uitgevoerd.";
?>


The problem:
The price in the CSV file is e.g 1,25 where as OpenCart expects 1.25.

Is there a way for the script to replace the comma with a dot before the query runs?

Answer
$price = str_replace(",", ".", $price);

// here your run your query
$result = mysql_query("UPDATE ..." ) or die(mysql_error());