CSV文件上传和更新表数据到MySQL [循环问题]

时间:2015-02-03 07:19:09

标签: php mysql csv prestashop-1.6

尝试更新(如果产品价格发生变化)并使用下面的代码在产品表中插入新产品。

$connection = mysql_connect(_DB_SERVER_, _DB_USER_, _DB_PASSWD_);
$dbs_select = mysql_select_db(_DB_NAME_);

// read the csv file in the form of array
function readCSV($csvFile) {
    $file_handle = fopen($csvFile, 'r');
    while (!feof($file_handle)) {
        $line_of_text[] = fgetcsv($file_handle, 1024);
    }
    fclose($file_handle);
    return $line_of_text;
}

//Set path to CSV file
$csvFile = '/home/demorct/public_html/prestashop/first import.csv';
$csv = readCSV($csvFile);
// CSV PArsing
$flag = 0;
foreach ($csv AS $csvdata) {
   $flag = $flag + 1;
   if($flag ==1){
       continue;
   }
    $csvdata1 = $csvdata['0'];
    $dataparsing = explode(";", $csvdata1);
    $BrandName = $dataparsing[0];
    $Modele = $dataparsing[1];
    $Ome = $dataparsing[2];
    $Gamme = $dataparsing[3];
    $Article = $dataparsing[4];
    $Abregee = $dataparsing[5];
    $Designation = $dataparsing[6];
    $Contenance = $dataparsing[7];
    $Page = $dataparsing[8];
    $Color = $dataparsing[9];
    $Weight = $dataparsing[10];
    $Code = $dataparsing[11];
    $Packagingimage = $dataparsing[12];
    $Productimage = $dataparsing[13];
    $Famille = $dataparsing[21];
    $Finalprice = $dataparsing[24];
    $status = $dataparsing[25];
    $quantity = $dataparsing[26];
    $date = date('Y-m-d H:i:s');  

  echo $productselect = "select  * from `ps_product` where `price` <> '$Finalprice' or `reference` <> '$Article' or `ean13` <>  '$Code' or `is_referenceoem` <> '$Ome' or `Range` <> '$Gamme' or `PackagingImage` <> '$Packagingimage' or `DesignationResume` <> '$Designation' or `Containance` <>  '$Contenance' or `Capacity` <> '$Page' or `quantity` <> '$quantity' or `color` <> '$Color' or `modele` <> '$Modele'";
          $result =  mysql_query($productselect) or die(mysql_error());
          while($dataArray = mysql_fetch_assoc($result)){
              echo "<pre>";
           echo $test = $dataArray['id_product'];
                $existrate = $dataArray['price'];
                $reference = $dataArray['reference'];
                $ean13 = $dataArray['ean13'];
                $is_referenceoem = $dataArray['is_referenceoem'];
                $Range = $dataArray['Range'];
                $PackagingImage = $dataArray['PackagingImage']; 
                $Containance = $dataArray['Containance'];
                $DesignationResume = $dataArray['DesignationResume'];
                $Capacity = $dataArray['Capacity'];
                $quantity = $dataArray['quantity'];
                $color = $dataArray['color'];
                $modele = $dataArray['modele'];
             echo "</pre>";

//MANUFACTURE ID
    $select_10 = "select * from `ps_manufacturer`";
    $result_10 = mysql_query($select_10);
    while ($row10 = mysql_fetch_array($result_10)) {
        $id_manufacturer = $row10['id_manufacturer'];




               echo "<br/>Final = $Finalprice, Existing = $existrate <br />"; 


          if (( $existrate!= $Finalprice ) && ($reference!= $Article) &&  ( $ean13!=$Code ) &&  ($is_referenceoem!= $Ome )){



         $mysql = "INSERT IGNORE  INTO `ps_product` (`id_category_default` ,`id_manufacturer`,`reference`,`ean13`,`is_referenceoem`,`price`,`PackagingImage`,`DesignationResume`,`Containance`,`Capacity`,`Range`,`date_add`,`quantity`,`color`,`modele`) VALUES(1,$id_manufacturer,'$Article','$Code','$Ome','$Finalprice','$Packagingimage','$Designation','$Contenance','$Page','$Gamme','$date','$quantity','$Color','$Modele')    ";
        mysql_query($mysql) or die(mysql_error());

          } else { echo $rateUpadte = "UPDATE `ps_product` SET `price` = '$Finalprice' , `reference` = '$Article' ,`ean13` = '$Code' ,`is_referenceoem` = '$Ome' WHERE `id_product` = ".$value;
            mysql_query($rateUpadte) or die(mysql_error());


          } 
    }

          }
          $flag++;   
 } 

产品价格必须按照csv文件

CSV文件示例:

产品ID产品价格
1 10
2 50
3 28
4 48
5 69

目前db表中的产品表中的价格更新,但某处循环无法按预期工作。代码有效,但它会多次打印产品价格,有时会显示上一个产品的最后价格,或者在查询运行时显示00。

下面给出的输出示例......

一次查询运行并在表格中插入数据为...(在大多数情况下)

产品ID产品价格
1 00
2 00
3 00
4 00
5 00

一次查询运行并在表中插入数据为......(某些时间)
产品编号产品价格
1 69
2 69
3 69
4 69
5 69

0 个答案:

没有答案