2013-07-13 50 views
0

我有一個PHP CODE,看起來像下面那樣。這個想法是讓用戶能夠輸入一個金額,當點擊update項目將更新。但問題是,我的代碼無法正常工作。金額不增加或減少

PHP

<?php 
session_start() 
?> 

<?php 
if(isset($_POST['pid']) && isset($_POST['length']) && isset($_POST['Qty']) && isset($_POST['Category'])){ 

$pid = $_POST['pid']; 
$length = $_POST["length"]; 
$qty = $_POST['Qty']; 
$Category = $_POST['Category']; 
    **They are more codes below in this PHP tag which just check if the 
    item the user is adding is already in the basket and if not it should add it.** 

    } 
?> 

調整項目的數量(這是我有問題的代碼)

<?php 

if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { 
    // execute some code 
    $item_to_adjust = $_POST['item_to_adjust']; 
    $quantity = $_POST['quantity']; 
    $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers 
    if ($quantity >= 100) { $quantity = 99; } 
    if ($quantity < 1) { $quantity = 1; } 
    if ($quantity == "") { $quantity = 1; } 
    foreach ($_SESSION["cart_array"] as $array_key=>$each_item) { 
       if ($each_item['item_id'] == $item_to_adjust && $each_item['length'] == $length && $each_item['Category'] == $Category) { 
         $_SESSION["cart_array"][$array_key]['quantity']+=$quantity; 
        } 
       } 
} 
?> 

HTML

<form action="check.php" method="post"> 
<input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /> 
     <input name="adjustBtn' . $item_id . '" type="submit" value="Update" /> 
     <input name="item_to_adjust" type="hidden" value="' . $item_id . '" /> 
     </form> 

I HAVE TRY

echo$quantity,看看,雖然我已經進入爲合格量和它。所以我知道部分代碼正在工作。

問題 我認爲這事與我foreach聲明,但我不能找到它。我嘗試瞭解我所知道的一切,但我找不到任何東西。

請幫幫我。

+0

你有第二塊代碼之前的'session_start'嗎? – andrewsi

+0

@andrewsi是的我有 – Amy

+1

- 第一個代碼如何連接到第二個代碼? –

回答

0

您在表單中缺少類別和長度值。 在目前的情況下,您在foreach中的狀態會變得錯誤。

+0

我加入這樣的形式'' – Amy

+0

是的 - 類別和長度 - 但是你也需要這些數據,因爲這決定了你的產品(id-category-length) –

+0

所以我需要在調整數量php標籤中發佈'category-length' ? – Amy