dsfdsf23423 2019-04-27 21:42
浏览 50

购物车中的会话创建问题

In my code for shopping cart, sessions are not formed dynamically. I want to create session in this page and use sessions on the other page for item cart details.

How do I solve this problem?

<form  method="post" action="men.php?action=add&id=<?php echo 
        $product['id']; ?>"> 
      <img src="<?php echo $product['image'];?>">
      <img class="hover-img" src="<?php echo $product['h_image'];?>" >
      <p><?php echo $product['price'];?></p>
      <h6><?php echo $product['name'];?></h6>
      <input type="hidden" name="id" value="<?php echo $product['id']; ?>" />                                
      <input type="hidden" name="hidden_name" value="<?php echo 
        $product['name']; ?>" />
      <input type="hidden" name="hidden_price" value="<?php echo 
        $product['price']; ?>" />
      <input type="hidden" name="hidden_image" value="<?php echo 
        $product['image']; ?>" />
       Quantity <input type="text" class="form-control" name="qty" value="1" /><br>
       <input type="submit" name="cart" value="Add to Cart" />                       
   </form>

<?php
session_start();
if(isset($_POST['cart']))
{
// not working
     if(isset($_SESSION['shopping_cart']))
    {
        $count= count($_SESSION['shopping_cart']);
        $product_ids= array_column($_SESSION['shopping_cart'],'id');
        if(!in_array(filter_input(INPUT_GET,'id'),  $product_ids))
        {
            $_SESSION['shopping_cart'][$count]=array
            (
            'id'=> filter_input(INPUT_GET,'id'),
            'name'=> filter_input(INPUT_POST,'hidden_name'),
            'price'=> filter_input(INPUT_POST,'hidden_price'),
            'quantity'=> filter_input(INPUT_POST,'qty'),
            'image'=> filter_input(INPUT_POST,'hidden_image')
            );          
        }
        else
        {
             for($i=0; $i<count($product_ids); $i++)
            {
                if($product_ids[$i]==filter_input(INPUT_GET,'id'))
                {
                    $_SESSION['shopping_cart'][$i]['quantity']+= 
                     filter_input(INPUT_POST,'quantity');
                }
            } 
        }
    }
    else
    {           
    } 
}   
?>
  • 写回答

1条回答 默认 最新

  • douya2007 2019-04-27 22:26
    关注

    Sara. The session_start(); should be the first line in your document.

    So, it should look like:

    <?php session_start(); ?>

    and then the rest of your work

    NOTE : IF IT STILL NOT WORKING PLEASE PROVIDE THE ERROR.

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)