PHP购物车:如何使用会话变量更新购物车中的产品数量?

PHP购物车:如何使用会话变量更新购物车中的产品数量?,php,session-variables,shopping-cart,Php,Session Variables,Shopping Cart,我正在创建我的第一个购物车项目。但是在更新产品数量时遇到了问题 我想使用会话变量更新产品数量。有谁能帮我写代码,并告诉我怎么做吗 下面是my_cart.php页面代码 <tr> <th class="tablerow">Product Code</th> <th class="tablerow">Product Name</th> <th class="tablerow"&g

我正在创建我的第一个购物车项目。但是在更新产品数量时遇到了问题

我想使用会话变量更新产品数量。有谁能帮我写代码,并告诉我怎么做吗

下面是my_cart.php页面代码

    <tr>
        <th class="tablerow">Product Code</th>
        <th class="tablerow">Product Name</th>
        <th class="tablerow">Image</th>
        <th class="tablerow">Quantity</th>
        <th class="tablerow">Price</th>
        <th class="tablerow">Total Price</th>
        <th class="tablerow">Action</th>                 
    </tr>

    <?php
    $grand_total = 0;                                   // For Calculating Grand Price
    foreach($_SESSION['cart'] as $id=>$quantity)
    {
        $sql="SELECT * FROM products WHERE id='$id'";
        $result=mysql_query($sql) or die("Error Message");
        while($row=mysql_fetch_array($result))
        {       
        $grand_total+= $row['product_price']*$quantity; // For Calculating Grand Price  
    ?>        

    <tr>                
        <td class="tablerow"><?php echo $row['id']; ?></td>
        <td class="tablerow"><?php echo $row['product_name']; ?></td>
        <td class="tablerow"><?php echo "<img height='50' width='50' src='admin/".$row['product_image']."'/>" ?></td>

        <form name="update_cart" action="update_cart.php" method="post">

        <td class="tablerow"><input type="text" name="quantity" value="<?php echo $quantity; ?>" /><br /><input type="image" src="admin/images/updatecart.png" name="Update" value="Update" /></td>

        </form>

        <td class="tablerow"><?php echo $row['product_price']; ?></td>
        <td class="tablerow"><?php echo $quantity*$row['product_price']; ?> </td>
        <td class="tablerow"><?php print "<a href='delete_cart.php?pid=".$row['id']."'><img src='admin/images/delete.png'></a>"; ?></td></form>
    </tr>

    <?php 
        }
    }
    ?>

    <tr>
        <td class="tablerow" colspan="7">Grand Total: Rs <?php echo $grand_total; ?></td>
    </tr>       

    <tr>
        <td class="tablerow" colspan="7"><?php print "<a href='clear_cart.php'><img src='admin/images/clearcart.png'></a><a href='http://localhost/Shopping-Cart/front-end/'><img src='admin/images/continueshopping.png'></a><a href='update_cart.php'><img src='admin/images/placeorder.png'></a>";?></td>
    </tr>              

    </table>

产品代码
品名
形象
量
价格
总价
行动
总计:卢比
我想使用会话变量更新产品数量

有一个问题:您想从哪里在客户端或服务器上更新qnt(您的会话变量存储在服务器上)? 我可以假设,您希望您的客户在其浏览器(客户端)中更改qnt,而服务器应该知道? 如果是这样,您需要在客户端上使用JS+Ajax请求(Jquery更简单的方式),它将Ajax请求发送到文件(例如:change_qnt.php),如果成功,将在客户端(浏览器)上更新qnt

change_qnt.php将更改存储qnt的会话变量,然后根据您在访问者页面上是否更新qnt返回结果(如果成功)

非常简单的例子:

<script>
$('selector_that_will_change_qnt').click() {
.ajax ({
url: 'change_qnt.php',
type: 'post',
dataType: 'html',
success: function() { // code here if success request},
error: function(){ //code here if request was failed}
});
}
</script>

$(“将更改的选择器”)。单击(){
.阿贾克斯({
url:“change_qnt.php”,
键入:“post”,
数据类型:“html”,
success:function(){//如果请求成功,请在此处编写代码},
错误:函数()
});
}

在会话数组中保存所有产品ID。索引应与产品id相同。这意味着如果用户单击或添加新产品,则在数组中的位置
$\u SESSION['cart']['product\u id']
保存1。如果用户再次添加相同的产品,则只需执行
$\u会话['cart']['product\u id']+
,然后您就可以使用
foreach($\u会话['cart']作为$id=>$quantity)打印数组。
。您可以轻松地编写上述概念的代码

这里有一个例子,如果你能从中理解任何东西

if(isset(isset($_REQUEST['pid']) && !empty($_REQUEST['pid'])) {
    $_SESSION['item'] = $_SESSION['item'] + 1; //Total number of items in cart
    $p_id = $_REQUEST['pid']; //Clicked product's id
    $_SESSION['cart'][$p_id]++; //Increment in relevant index value by one
    $cost_query = "select * from product where id=$p_id";
    //Calculate cost here and store it in session variable to use in your main page or where you are displaying your cart
}

我已经在add_cart.php页面中完成了这项工作。下面是代码“session_start();如果(isset($_POST['Buy']){$pid=$_POST['id'];$_SESSION['cart'][$pid]+=1;header(“location:my_cart.php?msg=Product Added Successfully”);}但如何更新数量?看,我用了一个表格。但我不确定这样是否可以更新数量。@user3252609您知道Ajax吗?如果是,则在每次单击“购买产品”后,将“$\u SESSION['cart'][$pid]”值更新1。Ajax会将产品的id发送到另一个页面,如果设置了id,则在客户端使用会话变量将相关产品id索引的数组值更新1。下面是add_cart.php页面会话_start()的代码;if(isset($_POST['Buy']){$pid=$_POST['id'];$_SESSION['cart'][$pid]+=1;标题(“位置:my_cart.php?msg=Product Added Successfully”);}