使用PHP + MySQLi的UPDATE查询有效,但始终在数量字段上设置0

时间:2018-08-14 14:23:11

标签: php mysqli prestashop

我对这个更新查询感到绝望。

我需要在Prestashop中更新很多产品的数量,并且我正在使用一些带有数量和ID的PHP脚本和XML来做到这一点。

<?php

    $ruta="XML/".$_FILES['uploadedfile']['name'];   
    $prueba = simplexml_load_file($ruta);
    foreach ($prueba as $libro):

//From XML
    $id=$libro->vendorListingid; //From XML
    $quantity=$libro->quantity['amount'];

//The XML have 6 digits with 0
    $id=ltrim($id, "0");  

// Relationship between the XML and the database
    $consulta3 = "SELECT id_product from ps_product WHERE reference='$id';"; 
    $rows=mysqli_query($con_presta,$consulta3);
    while ($row = mysqli_fetch_row($rows)){ 
        echo $idfinal=$row[0]; 
    }

//THE QUERY
     echo $consulta = "UPDATE ps_product SET quantity=$quantity WHERE id_product=$idfinal;";



// LAUNCH!
    if ($con_presta->query($consulta) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $con_presta->error;
    }

   endforeach;
?>

结果正常,回显$ consulta为=“ UPDATE ps_product SET ps_product.quantity ='2'WHERE ps_product.id_product ='1';”对于第一个产品(作为示例),但是脚本设置为0并且查询正常,因为它在PHPMyAdmin中可以正常工作

Table Definition

1 个答案:

答案 0 :(得分:1)

尝试:

$stock = StockAvailable::getQuantityAvailableByProduct((int) $idfinal, 0, 1);
if ($stock != $quantity) {
     StockAvailable::setQuantity((int) $idfinal, 0, (int) $quantity, 1);
}

致谢