我使用 phpwebcommerce 的购物车教程中的代码,我正在尝试添加一个文本字段供用户插入数量。由于单个产品有不同数量的选择,所以我使用 while 语句来查询子项目,并在每个项目旁边放置一个文本字段和一个添加按钮。但它只需要第一项的数量,而不是其余的。如果我为第一个项目插入一个数量,然后在任何其他项目上单击“添加”,它仍会将第一个存储在我的购物车中,它不应该......我被卡住了......有人可以帮我吗?
<?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> 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']);
}