duangu9173 2018-07-21 10:10
浏览 222
已采纳

购物车系统连接到数据库

I currently have a working cart system that when clicking "addToCart" the product code is stored inside $_SESSION['cart'] array and then printed out in a drop down menu, if no value is stored then it prints "No items in cart". This works fully fine but when I try to use the product code to pull the rest of the items data from the database I get 500 internal server error, however this method of connection to the database works perfectly fine, Any advice would be grateful.

<div class="dropDownCart">
        <button onclick="dropDownCart()" class="dropDownCartBtn">Cart</button>
        <div id="dropDownCartID" class="dropDownCartContent">
            <?php
            if ($_SESSION['cart'] === null) {
                echo "<span class='noItems'>No items in cart</span>";
            } elseif (!isset($_SESSION['cart'])) {
                echo "<span class='noItems'>No items in cart</span>";
            } else {
                require_once("dbConnection.php");
                $dbConn = getConnection();
                foreach ($_SESSION['cart'] as $productCode) {
                    $sqlQuery = "SELECT Name FROM studentcomforts WHERE Product_Code = $productCode";
                    $queryResult = $dbConn->query($sqlQuery);
                    $rowObj = $queryResult->fetchObject();
                        echo "<span class='cartItem'>
                                $rowObj->Name
                            </span>";
                }
            }
            ?>
  • 写回答

1条回答 默认 最新

  • doupo2633 2018-07-22 02:30
    关注

    Well you should check your web server error log for the exact error. The error log is usually inside the /var/log/apache2 folder. The following may help:

    You need to initialize the Php session using session_start(). session_start should be called before the first if statement.

    If your product code is a string, then it should be enclosed in quotes, so your sql query should be: "SELECT Name FROM studentcomforts WHERE Product_Code = '$productCode'";

    You can combine the if and first else statement. For example:

    if ($_SESSION['cart'] === null || !isset($_SESSION['cart']))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?