I need help to eliminate the errors Im getting. I have a shopping cart that is displayed on the products page by way of a cart update page. I get the below pair of errors (Notices) for every product listed:

Notice: Undefined variable: obj in C:\xampp\htdocs\Project2\browseproducts.php on line 89

Notice: Trying to get property of non-object in C:\xampp\htdocs\Project2\browseproducts.php on line 89

When I click on any of the Add to Cart buttons, nothing is added to the cart and I get:

Notice: Undefined index: Stock in C:\xampp\htdocs\Project2\cart_update.php on line 25

My code is:

 <?php
            $user = "root";
            $password = "";
            $database = "WebStore";
            mysql_connect("localhost", $user, $password);
            @mysql_select_db($database) or die("Unable to select database");
            $current_url = base64_encode("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
            $query = "SELECT * FROM PRODUCTS ORDER BY Category";
            $result = mysql_query($query);

            if($result === FALSE) {
            die(mysql_error());} // TODO: better error handling    

            // keeps getting the next row until there are no more to get

            while ($row = mysql_fetch_array($result)) {
                echo "<tr>";
                echo "<form method='post' action='cart_update.php'>";
                echo "<td>" . "<img src=" .$row['IconURL'] . "alt=" .$row['Name'] ."/>" . "</td>";
                echo "<td>" . $row['Name'] . "</td>";
                echo "<td>" . $row['Descr'] . "</td>";
                echo "<td>" . '$' . $row['Price'] . "</td>";
                echo "<td>" . $row['Stock'] . "</td>";
                echo "<td> <button class='add_to_cart'>Add to Cart</button> </td>";
        echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
        echo '<input type="hidden" name="type" value="add" />';
        echo '<input type="hidden" name="return_url" value="'.$current_url.'" />'; 
                echo "</form>";
                echo "</tr>\n";
            }

            mysql_close();
            ?>
        </table>

Some of my Cart Update code:

//add item in shopping cart
if(isset($_POST["type"]) && $_POST["type"]=='add')
{
    $product_code   = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
    $Stock    = filter_var($_POST["Stock"], FILTER_SANITIZE_NUMBER_INT); //product code
    $return_url     = \base64_decode($_POST["return_url"]); //return url

    //limit quantity for single product
    if($Stock < 1){
        die('<div align="center">We are currently out of stock<br /><a href="browseproducts.php/">Back To Products</a>.</div>');
    }
    $queryy = "SELECT TOP 1 Name,Price FROM Products WHERE product_code='$product_code'";
    //MySqli query - get details of item from db using product code
    $results = mssql_query($queryy);
    $obj = mssql_fetch_object($results);

Aaaany help would be appreciated!

In your code you use both mssql_* and mysql_* functions. Do you use two different database servers or is that just a typo from copying from some examples?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.