0

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 Answer 1

0

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']))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.