Disable "Add to Cart" button if the user already purchased that item from the Database

715 Views Asked by At

I have been working on this issue for the past couple days now and it's been driving me nuts, so I thought I would bring the question at hand here. Please note that I am still learning php :) so this is a practice project.

I am currently creating a mockup registration page where users of the registration page can signup, purchase items, and see others who purchased an item (this is intentional). My current issue is that I want to disable the user from purchasing an item that they already purchased by disabling the "add to cart" button if the item for that user exists in the database already. Basically, a buyer can only purchase 1 item from the database and when they try to purchase another one by going to the store, the "add to cart" button would be disabled for them.

Note: When I originally made this (about 2 months ago) I was only using MySQL but then learning MySQLi I started (very slowly) converting it. The database file has both connections for MySQL and MySQLi with it, so it allowed me to utilize both at the same time as I learned and went along.

<?php
session_start(); // Starts Session to carry Variables
include_once('THIS IS WHERE THE SQL FILE GOES. I REMOVED IT'); //Connects page to Database

/* Verifies user is logged in, if they are not redirect them to index.php */
if($loggedin != 1)
{
    header("Location: index.php");
}

/* Category Feature. Allows Page to be Sorted by sort column */
if (isset($_POST['searchcatbtn'])){ 

}

include("header.php") /* Includes Header */
?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
</head>
<body>

<!-- Creates Products within a Div provided by a Style Sheet-->
<div id="products-wrapper">
<h1>Products</h1>
<div class="products">

    <?php
    //current URL of the Page. cart_update.php redirects back to this URL
    $current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

    $Qcategories = mysql_query("SELECT DISTINCT sort FROM events ORDER BY sort ASC;");//Get all data from the users table sorts list alphabetically by username
    $results = $mysqli->query("SELECT * FROM events ORDER BY eventid");// WHERE sort = '$sort' 
    $remaining = $mysqli->query("SELECT * FROM orders");
    $obj_r = $remaining->fetch_object(); // obj_r means objects remaining
    $category = $obj->sort;


    if ($results) { 

        //fetch results set as object and output HTML
        while($obj = $results->fetch_object())
        {
            echo '<div class="product">'; 
            echo '<form method="post" action="cart_update.php">';
            echo '<div class="product-thumb"><img src="images/'.$obj->image.'"></div>';
            echo '<div class="product-content"><h4>'.$obj->name.'</h4>';
            echo '<div class="product-desc">'.$obj->description.'</div>';
            echo '<div class="product-info">';
            echo 'Price: '.$currency.$obj->price.' | ';
            echo 'Items Left: '.$obj->max.' | ';
            echo '<input type="hidden" style="width:20px;height:15px;" type="text" name="product_qty" value="1" size="3" />'; // OLD CODE TO DISPLAY QTY BOX: Qty: <input style="width:20px;height:15px;" type="text" name="product_qty" value="1" size="3" />';
            echo '<button class="add_to_cart">Add To Cart</button>';
            echo '</div></div>';
            echo '<input type="hidden" name="product_code" value="'.$obj->name.'" />';
            echo '<input type="hidden" name="type" value="add" />';
            echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
            echo '</form>';
            echo '</div>';
        }

    }
    ?>

</div>


<!-- Creates Cart within a Div provided by a Style Sheet-->
<div class="shopping-cart">
<h2>Your Shopping Cart</h2>
    <?php
    if(isset($_SESSION["products"]))
        {
        $total = 0; //sets default vaule of total
        echo '<ol>';
        foreach ($_SESSION["products"] as $cart_itm) /* Calls Session and references it as cart_itm. Creates items in the cart per item. If empty, shows empty cart*/
            {
                echo '<li class="cart-itm">';
                echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
                echo '<h3>'.$cart_itm["name"].'</h3>';
                echo '<div class="p-code">Product code : '.$cart_itm["code"].'</div>'; //safe to delete this if you want
                echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
                echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
                echo '</li>';
                $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
                $total = ($total + $subtotal);
            }


        echo '</ol>';
        echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a href="view_cart.php">Check-out!</a></span>'; /* total amount of cart */
        echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'">Empty Cart</a></span>';
        }
    else
        {
            echo 'Your Cart is empty';
        }
    ?>
<br><br>    

<!-- Creates categories. Allows page to be sorted by Category -->
<h2>Categories</h2>
<form name="searchcat" action="store.php" method="POST">
<div align="center">
<select name="mydropdown">

<?php               
while($row = mysql_fetch_array($Qcategories))//loops through $Query row by row
    {
        echo '<option value="' . $row['sort'] . '">' . $row['sort'] . '</option>'; //prints the categories from looping though the array
    }               
?>  
<input type="submit" name="searchcatbtn" class="btn btn-success" value="Submit Edits" />    
</form>
</select>
</div>
</div>

</body>

</html>
0

There are 0 best solutions below