I have a PHP class called Cart and in my PHP file, I need to call the checkCart() function to check if there is an item in the cart with the same id as the item_id of the product that the button is assigned to and if the user_id in that row is the same as the $_SESSION['user_id']. Basically trying to create an if statement that will check if the product is already in the cart database and if it is and it is assigned to the user that is currently in session the button will be displayed as Item already in the cart and if it isn't the button will display Add To cart;
<?php
// php cart class
class Cart
{
public $db = null;
public function __construct(DBController $db)
{
if (!isset($db->con)) return null;
$this->db = $db;
}
// $item_cart is the product id from database
public function checkCart($item_cart){
$select =$this->db->con->prepare("SELECT cart_id FROM cart WHERE user_id=:user-id AND item_id=:item-cart");
$select->bindParam(':user-id', $_SESSION('user_id'));
$select->bindParam(':item-cart', $item_cart);
$select->execute($_SESSION['user_id'],[$item_cart]);
if($select->rowCount() > 0){ ?>
<input type="hidden" name="item_id" value="<?php echo $item_cart; ?>">
<input type="hidden" name="user_id" value="<?php echo $_SESSION['user_id'] ?>">
<button type="submit" name="webshop_submit">Add To Cart</button>
<?php } else { ?>
<input type="hidden" name="item_id" value="<?php echo $item_cart; ?>">
<input type="hidden" name="user_id" value="<?php echo $_SESSION['user_id'] ?>">
<button style="background: #7A7A7A" type="submit" disabled>Item already in cart</button>
<?php }
}
}