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 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决