0

This is code to enable the "add to cart" button when the user logs in to my site. But I get an error:

Warning: Undefined variable $product_check_result in C:\xampp\htdocs\store\check_if_added.php on line 9

Could someone explain?

<?php

  function check_if_added_to_cart($item_id){
    

       require 'connection.php';
       
       $user_id=$_SESSION['id'];
       $product_check_query = $con->prepare("SELECT * from users_items WHERE item_id= :item_id AND user_id= :user_id AND status = :status");
       $product_check_result->execute(array(':item_id' => $item_id, ':user_id' => $user_id, ':status' => 'Added to cart'));
      
       $count = $product_check_result->fetchAll();
       if(count($count) > 0) {
       return true;
       }
       else {
       return false;
       }
   }
?>
Paul T.
  • 4,703
  • 11
  • 25
  • 29
  • 1
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – catcon May 10 '21 at 00:46

1 Answers1

0

Try this

...
$user_id=$_SESSION['id'];

$product_check_query = $con->prepare("SELECT * from users_items WHERE item_id= :item_id AND user_id= :user_id AND status = :status");

$product_check_query->execute(array(':item_id' => $item_id, ':user_id' => $user_id, ':status' => 'Added to cart'));

$product_check_result = $product_check_query->fetchAll();
      
$count = count($product_check_result);
...

It's pretty much the Example #1 from the official fetchAll() docs.

Skacc
  • 1,726
  • 2
  • 12
  • 16