php购物车数量更新与链接

时间:2011-12-14 14:11:22

标签: php mysql session

我想使用链接(锚点)来更新我的购物车,而不是按钮,例如:

<a href="shoppingcart.php?command=update&product<?=$id?>=<?=$q?>">update</a>

然而,这不会更新我的购物车。我怎样才能让它发挥作用?

我的标题:

if($_REQUEST['command']=='add' && $_REQUEST['id']>0){
    $id=$_REQUEST['id'];
    sepetle($id,1);
}        
else if($_REQUEST['command']=='delete' && $_REQUEST['id']>0){
    remove_product($_REQUEST['id']);
}
else if($_REQUEST['command']=='clear'){
    unset($_SESSION['cart']);
}
else if($_REQUEST['command']=='update'){
    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++){
        $id=$_SESSION['cart'][$i]['productid'];
        $q=intval($_REQUEST['product'.$id]);
        if($q>0 && $q<=999){
            $_SESSION['cart'][$i]['qty']=$q;
        }
    }
}

我的HTML代码:

if(is_array($_SESSION['cart'])){
    echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Serial</td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td><td>Options</td></tr>';
    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++){
        $id=$_SESSION['cart'][$i]['productid'];
        $q=$_SESSION['cart'][$i]['qty'];
        $pname=get_product_name($id);
        if($q==0) continue;
    }
}

我的链接:

<a href="shoppingcart.php?command=clear&id=<?=$id?>">Clear</a>
<a href="shoppingcart.php?command=update&product<?=$id?>=<?=$q?>">update</a>

我的表:

<tr bgcolor="#FFFFFF"><td><?=$i+1?></td><td><?=$pname?></td>
    <td>$ <?=get_price($id)?></td>
    <td><input type="text" name="product<?=$id?>" value="<?=$q?>" maxlength="3" size="2" /></td>                    
    <td>$ <?=get_price($id)*$q?></td>
    <td> <a href="shoppingcart.php?command=delete&id=<?=$id?>">Remove</a></td>
</tr>

2 个答案:

答案 0 :(得分:0)

您的代码中的问题是网址,您不应该使用<?php ?>包装变量,只需将变量放在'. ... .'之间,如:

<a href="shoppingcart.php?command=update&product='.$id.'">update</a>

答案 1 :(得分:0)

如果您的购物车中没有任何内容,$max变量将为零,那么不是吗?因此for循环不会运行,也不会将任何内容添加到您的购物车中。查看代码创建的URL以及任何调试输出都会有所帮助。