添加到购物车按钮不在PHP中添加产品到购物车

时间:2018-05-27 15:51:20

标签: php mysql

我试图编写一个允许用户输入产品数量的购物篮,然后点击按钮添加到购物车。当我点击添加到购物车按钮但没有任何反应。代码如下所示:

<?php
session_start();
$connect = mysqli_connect("localhost", "root", "", "stock");

if(isset($_POST["add_to_cart"]))
{
    if(isset($_SESSION["shopping-cart"]))
    {
        $_item_array_id = array_column($_SESSION["shopping-cart"], 
        "item_id");
        if(!in_array($_GET["id"], $item_array_id))
        {
            $item_array = array(
                'item_id'           =>  $_GET["id"],
                'item_product'      =>  $_POST["hidden_product"],
                'item_price'        =>  $_POST["hidden_price"],
                'item_quantity'     =>  $_POST["quantity"]
            );
            array_push($_SESSION['shopping_cart'], $item_array);
        }
        else
        {
            echo '<script>alert("Item has already been added to your 
            shopping basket")</script>';
            echo '<script>window.location="ShoppingCart.php"</script>';
        }
    }
    else
    {
        $item_array = array(
            'item_id'           =>  $_GET["id"],
            'item_product'      =>  $_POST["hidden_product"],
            'item_price'        =>  $_POST["hidden_price"],
            'item_quantity'     =>  $_POST["quantity"]
        );
        $_SESSION["shopping-cart"][0] = $item_array;
     }
}

if(isset($_GET["action"]))
{
    if($_GET["action"] == "delete")
    {
        foreach($_SESSION["shopping_cart"] as $keys => $values)
        {
            if($values["item_id"] == $_GET["id"])
            {
                unset($_SESSION["shopping-cart"][$keys]);
                echo '<script>alert("Item removed from cart")</script>';
                echo '<script>window.location="ShoppingCart.php"</script>';
            }
        }
    }
}

?>

<!doctype html>
<html lang="en">
  <head>
  </head>

  <body>

  <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data- 
target="#navbarSupportedContent" aria-controls="navbarSupportedContent" 
aria- 
   expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

   <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="HomePage.php">Home <span class="sr-only"> 
   (current)</span></a>
      </li>
       <li class="nav-item active">
        <a class="nav-link" href="CDPage.php">Shop</a>
      </li>
      <li class="nav-item active">
        <a class="nav-link" href="About.php">About</a>
      </li>
    </ul>

    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" 
aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" 
type="submit">Search</button>
    </form>

 </div>
</nav>

<div class = "container>
        <?php
        $query = "SELECT * FROM stock ORDER BY ID ASC";
        $result = mysqli_query($connect, $query);
        if(mysqli_num_rows($result) > 0)
        {
            while($row = mysqli_fetch_array($result))
            {
            ?>
             <div class="col-md-4">
                <form method ="post" action = "shoppingcart.php? 
   action=add&id=<?php echo $row["id"];?>">
                    <div style="border:10px solid background-color:blue 
    border-radius:5px padding: 50px" align = "center">
                        <img src="<?php echo $row["image"];?>" class = "img- 
    responsive" ><br/>
                        <h4 class = "text-info"><?php echo $row["product"];? 
    ></h4>
                        <p class="text-info"><?php echo 
    $row["description"];?>
                    <h4 class = "text-danger">€ <?php echo $row["price"];?> 
    </h4>
                    <input type="text" name="quantity" class="form-control" 
    value = "1" >
                    <input type="hidden" name="hidden_product" value = "<? 
    php echo $row["product"];?>" >
                    <input type="hidden" name="hidden_price" value = "<?php 
    echo $row["price"];?>" >
                    <input type="submit" name="add_to_cart" style="margin- 
    top:5px ;" class="btn btn-success" value="Add To Cart" >
                </div>  
            </form>
        </div>
    <?php   
        }
    }
    ?>
        <div style="clear:both"></div>
        <br />
        <h3>Order Details</h3>
        <div class="table-responsive">
            <table class="table table-bordered">
                <tr>
                    <th width="40%">Product Name</th>
                    <th width="10%">Quantity</th>
                    <th width="20%">Price</th>
                    <th width="15%">Total</th>
                    <th width="5%">Action</th>
                </tr>
                <?php
                if(!empty($_SESSION["shopping-cart"]))
                {
                    $total = 0;
                    foreach($_SESSION["shopping_cart"] as $keys => $values) 
                    {
                    ?>
                        <tr>
                            <td><?php echo $values["item_product"]; ?></td>
                            <td><?php echo $values["item_quantity"]; ?></td>
                            <td>€ <?php echo $values["item_price"]; ?></td>
                            <td><?php echo 
    number_format($values["item_quantity"] * $values["item_price"], 2); ?> 
</td>
                        <td><a href="shoppingcart.php?action=delete&id=<?php 
echo $values["item_id"]; ?>"><span class="text-danger">Delete</span></a>
                    </tr>
                    <?php 
                        $total = $total + ($values["item_quantity"] * 
    $values["item_price"]);
                }
                ?>
                <tr>
                    <td colspan="3" align="right">Total</td>
                    <td align="right">€ <?php echo number_format($total, 2); 
    ?></td>
                    <td></td>
                </tr>
                <?php 

            }
            ?>
        </table>
        <a href="CheckoutPage.php" class="btn btn-success btn-lg active" 
role="button" aria-pressed="true">Checkout</a>
    </div>
</div> 

  </body>
</html>

在尝试此尝试之前,我几乎没有使用PHP编写代码的经验,所以我很确定问题就在那里。 我最初试图把购物车放在一个单独的页面上,但我根本无法得到它,这是我到目前为止最接近的。

0 个答案:

没有答案