2012-11-15 5 views
0

을 수동으로 소개 할 수있는 양식이있는 $ _SESSION [ 'cart'] 테이블을 가지고 있습니다. 수량 ($ _SESSION [ '카트'] 제품.Substract Session [카트] db의 재고 수량을

<form name="formulario2" method="POST" target="oculto"><input type="hidden" name="action" value="update"> 
    foreach($_SESSION['cart'] as $product_id => $quantity) { 
    echo "<td align=\"center\"><input type = \"text\" size=\"1\" name=\"qty[$product_id]\" value =\"{$_SESSION['cart'][$product_id]}\"></td>"; 
} 
</form> 

그때 나는 내가 가진 그 수량에 빼기을 부탁하여 ($ _SESSION [ '카트']) 수량 이제

<?php 
    if(isset($_POST['action']) && ($_POST['action'] =='update')){ 
    // 
    foreach ($_POST['qty'] as $product_id=> $quantity){ 
    $qty = (int)$quantity; 
    if ($qty > 0){ 
    $_SESSION['cart'][$product_id] = $qty; 
    } 
    } 
    } 
    ?> 

를 업데이트하려면 다음 사용 ($ _SESSION [ '장바구니에 업데이트되었습니다. '])를 재고 목록에있는 수량으로 변경하십시오.

나는 마지막에 "foreach는 ($ _POST는 [ '수량은']"나는 또한 데이터베이스 수량으로 업데이트 양을 빼줄 말을해야한다고 생각하지만 난 그것을 할. 어떤 도움하는 방법을 몰라?

+0

http://en.wikipedia.org/wiki/SQL_Update –

답변

0

1) value =\"{$_SESSION['cart'][$product_id]}\"value =\"{$quantity}\"으로 대체하십시오. 이미 foreach 성명서에서 검색했습니다. 당신이 MySQL을 사용하는 것이 갖는 데이터베이스의 2), 내가 PDO와 데이터베이스에 액세스 reccommend 것 (I 인해)과 일치하지 않는 괄호 들여 쓰기의 부족과 코드의 두 번째 블록을 다시 한 :

<?php 
    if ((isset($_POST['action']) && ($_POST['action'] == 'update')) 
    { 
    foreach ($_POST['qty'] as $product_id => $quantity) 
    { 
     $qty = intval($quantity); 
     $pid = intval($product_id); // ALSO use the intval of the $product_id, 
            // since it was in a form and it can be hacked 
     $_SESSION['cart'][$pid] = $qty; // NOTE: you need to also update the 
             // session`s cart with 0 values, or 
             // at least to unset the respective 
             // product: 
             // unset($_SESSION['cart'][$pid]) 
     if ($qty > 0) 
     { 
     // now update the DB: 
     $mysql_host = "127.0.0.1"; 
     $mysql_user = "root"; 
     $mysql_password = ""; 
     $mysql_database = "myShop"; 
     $dbLink = new PDO("mysql:host=$mysql_host;dbname=$mysql_database;charset=utf8", $mysql_user, $mysql_password, array(PDO::ATTR_PERSISTENT => true)); 
     $dbLink->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); 
     $query = $dbLink->prepare("update `products` set `stock` = `stock` - ? WHERE `productId` = ? limit 1"); 
     $query->execute(array($qty, $pid)); 
     } 
    } 
} 
?> 

희망을 그것은 당신을 위해 일합니다!

감사합니다.

+0

고마워요. 완벽하게 작동하고 있습니다. – user1757383