簡體   English   中英

多個商品未插入購物車

[英]multiple items are not inserting into cart

我設計了一個購物車系統。 當用戶單擊添加到購物車時,它將刷新頁面,而單擊提交后,則僅添加一項。 我嘗試一個接一個地添加項目,然后在單擊提交后僅添加一個項目。 我在某個地方犯了錯誤,但我不明白該怎么辦? 你能告訴我嗎?

代碼:Place-Order.php

<?php
session_start();
require_once("dbcontroller.php");
?>
<div class="container">
    <?php
      $db_handle = new DBController();
      if(!empty($_GET["action"])) {
      if(!empty($_POST["quantity"])) {
            $productByCode = $db_handle->runQuery("SELECT * FROM tblproduct WHERE code='" . $_GET["code"] . "'");
            $itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));
            if(!empty($_SESSION["cart_item"])) {
                if(in_array($productByCode[0]["code"],array_keys($_SESSION["cart_item"]))) {
                    foreach($_SESSION["cart_item"] as $k => $v) {
                            if($productByCode[0]["code"] == $k) {
                                if(empty($_SESSION["cart_item"][$k]["quantity"])) {
                                    $_SESSION["cart_item"][$k]["quantity"] = 0;
                                } 
                                $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
                            }
                    }
                } else {
                    $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                }
            } else {
                $_SESSION["cart_item"] = $itemArray;
            }
        }
    }
   ?>
 <div id="product-grid">
                                <div class="txt-heading">Products <a id="btnEmpty" href="reviewcart.php"><b>Submit</b></a></div>

                                 <?php
                                     $product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
                                     if (!empty($product_array)) { 
                                        foreach($product_array as $key=>$value){
                                   ?>
                                   <div class="product-item">
                                        <form method="post" action="place-order.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
                                            <div class="product-image"><img src="<?php echo $product_array[$key]["image"]; ?>"></div>
                                            <div><strong><?php echo $product_array[$key]["name"]; ?></strong></div>
                                            <div class="product-price"><?php echo "$".$product_array[$key]["price"]; ?></div>
                                            <div><input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAddAction" /></div>
                                        </form>
                                    </div>
                                    <?php
                                        }
                                     }
                                    ?>
                                </div>

reviewcart.php

    <?php
 error_reporting(E_ERROR | E_PARSE);
session_start();
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
    case "add":
        if(!empty($_POST["quantity"])) {
            $productByCode = $db_handle->runQuery("SELECT * FROM tblproduct WHERE code='" . $_GET["code"] . "'");
            $itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));

            if(!empty($_SESSION["cart_item"])) {
                if(in_array($productByCode[0]["code"],array_keys($_SESSION["cart_item"]))) {
                    foreach($_SESSION["cart_item"] as $k => $v) {
                            if($productByCode[0]["code"] == $k) {
                                if(empty($_SESSION["cart_item"][$k]["quantity"])) {
                                    $_SESSION["cart_item"][$k]["quantity"] = 0;
                                }
                                $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
                            }
                    }
                } else {
                    $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                }
            } else {
                $_SESSION["cart_item"] = $itemArray;
            }
        }
    break;

    case "remove":
        if(!empty($_SESSION["cart_item"])) {
            foreach($_SESSION["cart_item"] as $k => $v) {
                    if($_GET["code"] == $k)
                        unset($_SESSION["cart_item"][$k]);              
                    if(empty($_SESSION["cart_item"]))
                        unset($_SESSION["cart_item"]);
            }
        }
    break;
    case "empty":
        unset($_SESSION["cart_item"]);
    break;  
}
}
?>
    <div class="c-layout-sidebar-content ">
        <!-- BEGIN: PAGE CONTENT -->
        <div class="c-content-title-1 c-margin-t-30">
            <h3 class="c-font-uppercase c-font-bold c-center ">Place Order</h3>

            <div class="c-content-panel">

                <div class="c-body">
                    <div class="row">
                        <div class="col-md-12">
                            <table class="table table-condensed">
                               <div id="shopping-cart">
                                 <div class="txt-heading">Shopping Cart <a id="btnEmpty" href="reviewcart.php?action=empty">Empty Cart</a></div>
                                 <?php
                                     if(isset($_SESSION["cart_item"])){
                                        $item_total = 0;
                                    ?>  
                                <table class="col-md-12" cellpadding="10" cellspacing="1" >
                                    <tbody class="col-md-12">
                                        <tr>

                                             <form action="reviewcart.php" method="post">
                                            <th class="col-md-4" style="text-align:center;"><strong>Name</strong></th>
                                            <th class="col-md-4" style="text-align:center;"><strong>Code</strong></th>
                                            <th class="col-md-4" style="text-align:center;"><strong>Quantity</strong></th>

                                            <th class="col-md-3" style="text-align:center;"><strong>Action</strong></th>
                                        </tr>   
                                        <?php       
                                            foreach ($_SESSION["cart_item"] as $item){
                                        ?>
                                        <tr>
                                            <td style="text-align:center;border-bottom:#F0F0F0 1px solid;" >
                                             <strong><?php echo  $item["name"]; ?></strong>
                                             <input type="hidden" name="name" value="<?php echo $item['name']?>">
                                         </td>
                                         <td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
                                             <?php echo  $item["code"]; ?>
                                             <input type="hidden" name="code" value="<?php echo $item['code']?>">
                                            </td>
                                         <td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
                                             <?php echo  $item["quantity"]; ?>
                                             <input type="hidden" name="quantity" value="<?php echo $item['quantity']?>">
                                         </td>
                                         <!-- <td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
                                         <?php  $price=$_POST['price'] ; echo $price ?>
                                         </td> -->
                                         <td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
                                             <a href="reviewcart.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction">
                                              Remove Item
                                             </a>
                                         </td>
                                        </tr>

                                        <!-- <?php
                                            $item_total += ($item["price"]*$item["quantity"]);
                                            }
                                        ?>
 -->
                                        <tr>
                                            <td colspan="5" align=right><input type="submit" name="submit" value="submit" /></td>
                                        </tr></form>

                                    </tbody>
                                </table>        
                                <?php
                                    }
                                ?>
                                </div>

                            </table>
                        </div>
                    </div>

                </div>
            </div>

        </div>
    </div>

為什么不刷新頁面呢?

您可以將頁面上列出的每個項目都視為一個復選框,然后在提交時,只需將所有復選框中的項目添加到購物車中即可。 顯然,無需像復選框那樣設計它,只需使其在后端代碼中那樣工作即可。

然后只要確保每次您提交到購物車或離開頁面導航時,它都會首先提交表單(如果它不是空的),然后進行導航。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM