从循环更新库存avaible quanty mysqli

时间:2017-02-23 16:47:19

标签: php mysql arrays session

所以我有一个用户选择要购买哪种产品的阵列,然后在用户订购命令后,我想更新产品的新库存数量,所以如果用户订购5件大小适中的夹克,例如: medium:10,small:5我希望更新升级现在的夹克尺寸为中等...所以它将是中等:5,小:5 . But doesn't work the column is called尺寸and the value are(sizeX ):( avaibleQuantityForSizeX),(SIZEY):( avaibleQuantityforSizeY)likemedium:100,small:50

问题是我错误的代码更新是给我值0

首先我有这个数组$ _SESSION ['cart_array'] - 用户在购物车中选择的内容Array ( [0] => Array ( [item_id] => 9 [quantity] => 15 [pretProdus] => 6615 [sizeChange] => R5 [culoare] => Negru ) [1] => Array ( [item_id] => 11 [quantity] => 5 [pretProdus] => 755 [sizeChange] => R10 [culoare] => Albastru ) )

   //CHECK daca este vreun produs selectat in cosul de cumparaturi
if (!isset($_SESSION['cart_array'])) {
    header("location: index.php"); 
    exit(); 
    }
//START SESSION CART ARRAY
 if(isset($_SESSION['cart_array']) || count($_SESSION['cart_array']) > 0){
      print_r($_SESSION['cart_array']);
      $uniqueIdComanda = substr(str_shuffle(MD5(microtime())), 0, 15);//random key pentru comanda
      $cartTotal ="";
         foreach ($_SESSION["cart_array"] as $each_item) { 
        $item_id = $each_item['item_id'];
        // start select query stmt
        $stmt = $con->prepare('SELECT id, product_name, price, size, details FROM products WHERE id = ? LIMIT 1');
        $stmt->bind_param('i', $item_id);
        $stmt->execute();
        $stmt->bind_result($idSelect, $product_name, $price, $size, $details);//variabla pe care o vrei inlocuita prin bind_Result in loc de get_Result
        while ($stmt->fetch())    {
           $idSelect;
           $product_name; // faci acelasi lucru fara $row 
           $price;    
           $size;
           $details;    
        }
        $stmt->free_result();
        // stoop select stmt


//confirmare comanda    
    if(isset($_POST["trimiteComanda"])){
    $nume = $_POST["nume_client"];
    //blablabla
    $comanda = 'IDprodus: '.$item_id.' / produseNume: '.$product_name.' / cantitate: '.$each_item['quantity'].' /  ';
     $stmt = $con->prepare("INSERT into  comanda (pret_comanda) VALUES (?)");
        $stmt->bind_param("s", $nume);
        if($stmt->execute())            {
        echo "<strong>SUCCES COMANDA A FOST TRIMISA CU SUCCES</strong>";
        //UPDATE STOCK + VANDUTE
            //start SIZE ALEGE
        function sizesToArray($string){
            $sizesArray = explode(',',  $string);
            $returnArray = array();
            foreach($sizesArray as $size){
                $s = explode(':', $size);
                $returnArray[]=array('sizeChange' => $s[0], 'quantity' => $s[1]);
                    }
            return $returnArray;
            }
        function sizesToString($sizes){
            $sizesString = '';
            foreach($sizes as $size){
                $sizesString .= $size['sizeChange'].':'.$size['quantity'].',';
                }
            $trimmed =rtrim($sizesString, ',');
            return $trimmed;
        }
            //function ajustare cantintate
        echo 'SIZE:  '.$size.'<br>';
        $newSizes=array();
        $sizes = sizesToArray($size);
        echo ' alalbala: '.print_r($sizes);
        foreach($sizes as $size){
            if($size == $each_item['sizeChange']){
                $q = $size['sizeChange'] -  $each_item['sizeChange'];
                echo 'noulQ: '.$q;
                $newSizes[] = array('sizeChange' => $size['sizeChange'], 'quantity' => $q);
                }else {
                    $newSizes[]=array('sizeChange' => $size['sizeChange'], 'quantity' => $size['quantity'] );
                    }
                }
        $sizesString = sizesToString($newSizes);

            $stmtEdit = $con->prepare("UPDATE products SET size=? WHERE id =?");
            $stmtEdit->bind_param("ii", $sizesString, $item_id);
            if(!$stmtEdit->execute()){
                echo "EROARE NU S-A PUTUT MODIFICA PRODUSUL SELECTAT";
                $con->error;}
                }
                else{
                    echo 'merrge : .. ';}
    //stop SIZE ALEGE:-1
}//close CONFIRMARE COMANDA ISSET


}//CLOSE foreach

    }//close isset session

0 个答案:

没有答案