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']))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料