简体   繁体   English

每次单击添加到购物车按钮时,如何在会话数组中添加值?

[英]How can I add value in a session array every time I click add to cart button?

Everytime I click add cart button I pass an id value and get data from a database and I want to save it a session array. 每次我单击添加购物车按钮时,我都会传递一个id值并从数据库中获取数据,并且想要将其保存为会话数组。

<?php session_start() ?>
<?php require 'config.php' ?>
<?php 
  if (isset($_POST['id'])) {
    $sql = "SELECT product_name, product_price FROM products WHERE id=".$_POST['id'];   
    $result = $mysqli->query($sql) or die($mysqli->error);

    $result = $result->fetch_assoc();

    if (isset($_SESSION['cart'])) {
        if (isset($_SESSION['cart']['id'])) {
            unset($_SESSION['cart']['id']);
        }
    }

    $_SESSION['cart']['id'] = $result;

    $cart = count($_SESSION['cart']);
    //$cart = json_encode($_SESSION['cart']['id']);

    echo $cart;
  }
?>

Try something like this : 尝试这样的事情:

if(!isset($_SESSION['cart']))
    $_SESSION['cart'] = array();//Declaration of the cart
$_SESSION['cart'][] = $result;//To add the result in cart

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次单击提交按钮时如何为数组添加值? 在 php 中(带会话) - How to create add value to array everytime i click submit button? in php (with session) 如果我单击已在购物车中的产品的“添加到购物车”按钮,如何重定向到购物车页面 - How do I redirect to the cart page if I click on the 'Add to cart' button for a product that's already in cart 如何将每个产品作为新商品添加到购物车 - How can I add every product as a new item into cart 如何在每行上添加按钮 - How can I add a button on every line 我如何用图标替换“添加到购物车”文本(添加到购物车按钮woocommerce) - How can i replace text “Add to cart” with an icon (Add to cart button woocommerce) 如何更改链接“添加到购物车”按钮? - How can I change the link “add to cart” button? WooCommerce-如何使用具有相同购物车按钮的Ajax将多种不同的产品添加到购物车? - WooCommerce - How can I add multiple, different products to cart using ajax with same add-to-cart-Button? 我想使用laravel单击“添加到购物车”按钮时,将商品显示在购物车中吗? - I Want to show products items into shopping cart when i click add to cart button using laravel? 如何在 Woocommerce 中的 wc_add_to_cart_message 中添加一个额外的按钮? - How can I add an extra button the the wc_add_to_cart_message in Woocommerce? 单击添加到购物车时,如何自动将图像保存在网站内? - How can i automatically save the image inside a website when i click the add to cart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM