在magento数据库中更新权重

时间:2015-02-03 22:35:29

标签: php database magento

我正在尝试大量更新magento上的几千种产品,而且我在获取我写的脚本时遇到了一些问题。我添加了一个新表格,其中包含SKU和权重列表。然后我使用这个代码(下面)尝试遍历每一行,然后更新magento表中的权重。

这是我的代码,你可以看到我已经尝试了两种不同的方法,它没有出现错误或任何事情,但数据库中没有进行任何更改。

<?php
// DB Settings
$host = 'localhost';
$dbname = 'devdpbui_mgto';
$user = 'devdpbui_matt';
$pass = 'QuadRamR170';
 // Establish connection

 /*
// Select table with query
$sth = $dbh->query("
 SELECT sku,weight FROM temp_weight
");


// Set fetching mode
$sth->setFetchMode(PDO::FETCH_ASSOC);
 // Assign $row as your key to access Table fields
$i = 0;
 foreach ($sth as $row) :

if($i > 2){
 $sku = $row['sku'];
 $weight = $row['weight'];
 //print_r($row);
//print_r($weight);

 //MySqli Update Query
$query = "UPDATE dp_catalog_product_entity_decimal AS ped JOIN dp_eav_attribute AS ea ON  ea.entity_type_id = 10 AND ea.attribute_code = 'weight' AND ped.attribute_id = ea.attribute_id SET ped.value = ? WHERE ped.entity_id = ?";
if($statement = $dbh->prepare($query){
 print 'Success! ID of last inserted record is : ' .$statement->insert_id .'<br />'; 
}else{
    die('Error : ('. $dbh->errno .') '. $dbh->error);
}
if($statement->bind_param('is', $weight, $sku) {
     print 'Success! ID of last inserted record is : ' .$statement->insert_id     .'<br />'; 
}else{
    die('Error : ('. $dbh->errno .') '. $dbh->error);
}
//MySqli Delete Query
//$results = $mysqli->query("DELETE FROM products WHERE ID=24");

if($statement->execute()){
    print 'Success! ID of last inserted record is : ' .$statement->insert_id .'<br />'; 
}else{
    die('Error : ('. $dbh->errno .') '. $dbh->error);
} 

// $updateQtySQL = ;
 //echo $q->error;
 //$q = $dbh->prepare($updateQtySQL);
 //echo $q->error;
 //$q->execute(array($weight, $sku));
 //echo $q->error;
 //}

 $i++;


 endforeach;

$dbh = null;*/

?>
<?php


   $dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

// Select table with query
$sth = $dbh->query("
 SELECT sku,weight FROM temp_weight
");


// Set fetching mode
$sth->setFetchMode(PDO::FETCH_ASSOC);
 // Assign $row as your key to access Table fields
$i = 0;
 foreach ($sth as $row) :

if($i > 2){
 $sku = $row['sku'];
 $weight = $row['weight'];
$link = mysqli_connect("localhost", "devdpbui_matt", "QuadRamR170", "devdpbui_mgto");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
} else {
echo "ok";
  }
 /* create a prepared statement */
 if ($stmt = mysqli_prepare($link, "UPDATE dp_catalog_product_entity_decimal AS ped JOIN dp_eav_attribute AS ea ON  ea.entity_type_id = 10 AND ea.attribute_code = 'weight' AND ped.attribute_id = ea.attribute_id SET ped.value = ? WHERE ped.entity_id = ?")) {


    mysqli_stmt_bind_param($stmt, "is", $weight, $sku);

    /* execute query */
    mysqli_stmt_execute($stmt);


    /* close statement */
    mysqli_stmt_close($stmt);
}

/* close connection */
mysqli_close($link);

 endforeach;

$dbh = null;
?>`

0 个答案:

没有答案