Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

php - How to Add a Product in Cart

I am Getting Products from database and now i am trying to get that specific product that add by user in my session but i am unable to do that here is the my code

<div class="card" style="width: 18rem;margin: 5px;">
            <img src="<?php  echo $item['Pimage'] ?>" class="card-img-top" alt="...">
            <div class="card-body">
                <h5 class="card-title"><?php  echo $item['Pname'] ?></h5>
                <p class="card-text"><?php  echo $item['Pdesc'] ?></p>
                <P><?php  echo $item['Pprice'] ?>PKR</P>

                <form action="" method="post">
                <input type="hidden" value="<?php  $cid=$item['ID'] ?>">
                <input type="submit" class="btn btn-warning" name="cid" value="Add to Cart">
                <!-- <a href="sample.php" class="btn btn-warning">ADD To Cart</a>-->
                </form>
                <?php
                if(isset($_POST['cid']))
                {
                $name=$item['Pname'];
                $pdesc=$item['Pdesc'];
                $price=$item['Pprice'];
                $cartArray = array(
                    'name'=>$name,
                    'price'=>$pdesc,
                    'image'=>$price);
                $_SESSION['cart']=$cartArray;
                header('location:sample.php');
                }?>
            </div>
        </div>

on next page

<?php
session_start();
foreach($_SESSION['cart'] as $val)
{
    print_r($val);
}

here is my front end enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...