不能从除第一个文本框之外的其他文本框中获取值


Can't get the value from other textfield except the first one

我使用的代码从phpwebcommerce的购物车教程,我试图添加一个文本字段,供用户插入数量。由于单个产品有不同数量的选择,所以我使用while语句查询子项,并在每个子项旁边放置一个文本字段和一个add按钮。但是它只收第一项的数量,不收其他的。如果我为第一件商品插入一个数量,然后点击其他商品的"添加",它仍然会将第一件商品存储在我的购物车中,这是不应该的……我被困住了……有人能帮我一下吗?

<?php
function displayprodDetail($pdId)
{
    $query = mysql_query("SELECT * FROM product_image WHERE p_id ='$pdId'");
    WHILE($datarows = mysql_fetch_array($query))
    {
        $p_num = $datarows['p_number'];
        echo '<h1><span>'.$p_num.' -- $'.$datarows['price'].'</span></h1>';
        echo '<div id="prodPic"><a href ="'.$datarows['image']. '" rel="ibox"    title="'.$p_num.'"><img src ="'.$datarows['image'].'"/></a><br /></div>';
        echo '<div id="items">';
        $sql = mysql_query("SELECT * FROM items WHERE item_number LIKE '$p_num/%' ORDER BY item_number ASC");
        $numProduct = dbNumRows($sql);
        if($numProduct > 1)
        {
            echo '<table border="0" cellspacing="0">';
            WHILE($data = mysql_fetch_array($sql))
            {
                $itemId = $data['item_id'];
                $p_num = $data['item_number']; 
                echo '<tr>';
                echo '<td><INPUT TYPE="HIDDEN" NAME="'.$itemId.'" VALUE="'.$itemId.'">'.$p_num.'</td>';
                echo '<td>&nbsp;&nbsp;Qty: <input type="number" name="qty" id="qty" style="width:30px"></td>';
                $cart_url = "cart.php?action=add&i=$itemId&qty=";
                ?>
                <td><input type="Submit" name="Submit" value="Add" onClick="window.location.href='<?php echo $cart_url; ?>' +document.getElementById('qty').value;" ></td>
                <?php
                echo '</tr>';
            }
            echo '</table>';
        }
        else
        {
            echo 'Items are currently not available.<br>Contact us for more detail.';
        }
        echo '</div>';
    }
}
?>
下面是添加到购物车的函数:
function addToCart()
{
    // make sure the product id exist
    if (isset($_GET['i']) && (int)$_GET['i'] > 0) 
    {
        $itemId = (int)$_GET['i'];
    } 
    else 
    {
        header('Location: home.php');
    }
    if (isset($_GET['qty']) && (int)$_GET['qty'] > 0) 
    {
        $qty = (int)$_GET['qty'];
    } 
    // current session id
    $sid = session_id();
    // check if the product is already
    // in cart table for this session
    $sql = mysql_query("SELECT item_id FROM cart_table WHERE item_id = $itemId AND ct_session_id = '$sid'");
    if (dbNumRows($sql) == 0) 
    {
        // put the product in cart table
        $sql = mysql_query("INSERT INTO cart_table (item_id, ct_qty, ct_session_id, ct_date) VALUES ($itemId, $qty, '$sid', now())");
    } 
    else 
    {
        // update product quantity in cart table
        $sql = mysql_query("UPDATE cart_table SET ct_qty = ct_qty + $qty WHERE ct_session_id = '$sid' AND item_id = $itemId");                  
    }   
    // an extra job for us here is to remove abandoned carts.
    // right now the best option is to call this function here
    deleteAbandonedCart();
    header('Location: ' . $_SESSION['shop_return_url']);                
}

由于您的输入框命名相同,这就是为什么它不像预期的那样工作,尝试如下:

<input type="number" name="qty_<?php echo $itemId; ?>" id="qty_<?php echo $itemId; ?>" style="width:30px">...
...
<td><input type="Submit" name="Submit" value="Add" onClick="window.location.href='<?php echo $cart_url; ?>' +document.getElementById('qty_<?php echo $itemId; ?>').value;" ></td>