Isset 'add_to_cart' didnt work on only one page

Viewed 21

i cant solve this issue, only on one page addToCart didnt work properly.. my shop page code: see the include 'components/wishlist_cart.php';

<?php



session_start();

if(isset($_SESSION['user_id'])){
   $user_id = $_SESSION['user_id'];
}else{
   $user_id = '';
};



?>

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Shop | Garderoba.rs</title>

   <link rel="apple-touch-icon" sizes="180x180" href="images/apple-touch-icon.png">
   <link rel="icon" type="image/png" sizes="32x32" href="images/favicon-32x32.png">
   <link rel="icon" type="image/png" sizes="16x16" href="images/favicon-16x16.png">
   
   <!-- font awesome cdn link  -->
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">

   <!-- custom css file link  -->
   <link rel="stylesheet" href="css/style.css">

</head>
<body>

<?php
include 'components/connect.php';
**include 'components/wishlist_cart.php';**


$database = new mysqli('localhost', 'xyzdev', 'xyzdesign', 'shop_db_2');
$fetch_all_products = $database->query("SELECT * FROM products");
?>
<?php include 'components/user_header.php'; ?>

   <h1 class="heading">Shop</h1>

<section class="category">
 

   <div class="navigation_categories">

        <form id="search_form">

          <div class="box_menu">
            <h4>Kategorije:</h4>
            <menu>
                <input type="checkbox" name="filter_category[]" class="filter_value" value="1"> Muskarci <br>
                <input type="checkbox" name="filter_category[]" class="filter_value" value="2"> Zene <br>
                <input type="checkbox" name="filter_category[]" class="filter_value" value="3"> Sport <br>
            </menu>
          </div>  
         
        <div class="box_menu">
            <h4>Pod Kategorije:</h4>
            <menu>
                <!-- <input type="checkbox" name="filter_sub[]" checked class="filter_value"> Svi Proizvodi <br> -->
                <input type="checkbox" name="filter_sub[]" class="filter_value" value="1"> Majice <br>
                <input type="checkbox" name="filter_sub[]" class="filter_value" value="2"> Duksevi <br>
                <input type="checkbox" name="filter_sub[]" class="filter_value" value="3"> Trenerke <br>
                <input type="checkbox" name="filter_sub[]" class="filter_value" value="4"> Jakne <br>
                <input type="checkbox" name="filter_sub[]" class="filter_value" value="5"> Sport Patike <br>
                <input type="checkbox" name="filter_sub[]" class="filter_value" value="6"> LifeStyle Patike <br>
                <input type="checkbox" name="filter_sub[]" class="filter_value" value="7"> Lopte <br>
                <input type="checkbox" name="filter_sub[]" class="filter_value" value="8"> Fitnes <br>
                <input type="checkbox" name="filter_sub[]" class="filter_value" value="9"> Ranac <br>
            </menu>
        </div>

        <div class="box_menu">
            <h4>Brend:</h4>
            <menu>
                <input type="checkbox" name="filter_brand[]" class="filter_value" value="1"> Nike <br>
                <input type="checkbox" name="filter_brand[]" class="filter_value" value="2"> Adidas <br>
                <input type="checkbox" name="filter_brand[]" class="filter_value" value="3"> Reebok <br>
                <input type="checkbox" name="filter_brand[]" class="filter_value" value="4"> Puma <br>
                <input type="checkbox" name="filter_brand[]" class="filter_value" value="5"> Converse <br>
            </menu>
        </div>
        </form>
   </div>


</section>

<section class="products">
    <h1 class="heading">Proizvodi</h1>

   <div class="box-container"> 
        <?php if(isset($fetch_all_products) && is_object($fetch_all_products) && count($fetch_all_products))  {

                    foreach($fetch_all_products as $key => $products) { 
                
                ?>

            <form action="" class="box" method="post">
                <input type="hidden" name="pid" value="<?php $products['id'];  ?>">
                <input type="hidden" name="name" value="<?php $products['name'];  ?>">
                <input type="hidden" name="price" value="<?php $products['price'];  ?>">
                <input type="hidden" name="image" value="<?php $products['image_01'];  ?>">
                <button class="fas fa-heart" type="submit" name="add_to_wishlist"></button>
                <a href="quick_view.php?pid=<?= $products['id']; ?>" class="fas fa-eye"></a>
                <img src="uploaded_img/<?= $products['image_01']; ?>" alt="">
                <div class="name"><?= $products['name']; ?></div>
                <div class="flex">
                    <div class="price"><?= $products['price']; ?><span> /- rsd</span></div>
                    <input type="number" name="qty" class="qty" min="1" max="99" onkeypress="if(this.value.length == 2) return false" value="1">
                </div>
                <input type="submit" value="Dodaj u korpu" class="btn" name="add_to_cart">
            </form>


        
        <?php 
              } 
            
    }  
        
        
        
        ?>
   

   </div>

</section>

<?php include 'components/footer.php'; ?>

<script src="js/script.js"></script>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script>
    $(document).on('change', '.filter_value', function() {
        var url = "ajax.php";
        $.ajax({
            type: "POST",
            url: url,
            data: $('#search_form').serialize(),
            success: function(data) {
                $('.box-container').html(data);
            }
        });
        return false;
    });
</script>

</body>
</html>

Code of wishlist_cart.php :

if(isset($_POST['add_to_wishlist'])){

   if($user_id == ''){
      header('location:user_login.php');
   }else{

      $pid = $_POST['pid'];
      $pid = filter_var($pid, FILTER_SANITIZE_STRING);
      $name = $_POST['name'];
      $name = filter_var($name, FILTER_SANITIZE_STRING);
      $price = $_POST['price'];
      $price = filter_var($price, FILTER_SANITIZE_STRING);
      $image = $_POST['image'];
      $image = filter_var($image, FILTER_SANITIZE_STRING);

      $check_wishlist_numbers = $conn->prepare("SELECT * FROM `wishlist` WHERE pid = ? AND user_id = ?");
      $check_wishlist_numbers->execute([$pid, $user_id]);

      $check_cart_numbers = $conn->prepare("SELECT * FROM `cart` WHERE pid = ? AND user_id = ?");
      $check_cart_numbers->execute([$pid, $user_id]);

      if($check_wishlist_numbers->rowCount() > 0){
         $message[] = 'Proizvod je već dodat u listu želja!';
      }elseif($check_cart_numbers->rowCount() > 0){
         $message[] = 'Proizvod je već dodat u korpu!';
      }else{
         $insert_wishlist = $conn->prepare("INSERT INTO `wishlist`(user_id, pid, name, price, image) VALUES(?,?,?,?,?)");
         $insert_wishlist->execute([$user_id, $pid, $name, $price, $image]);
         $message[] = 'Dodato u listu želja!';
      }

   }

}

if(isset($_POST['add_to_cart'])){

   if($user_id == ''){
      header('location:user_login.php');
   }else{

      $pid = $_POST['pid'];
      $pid = filter_var($pid, FILTER_SANITIZE_STRING);
      $name = $_POST['name'];
      $name = filter_var($name, FILTER_SANITIZE_STRING);
      $price = $_POST['price'];
      $price = filter_var($price, FILTER_SANITIZE_STRING);
      $image = $_POST['image'];
      $image = filter_var($image, FILTER_SANITIZE_STRING);
      $qty = $_POST['qty'];
      $qty = filter_var($qty, FILTER_SANITIZE_STRING);

      $check_cart_numbers = $conn->prepare("SELECT * FROM `cart` WHERE pid = ? AND user_id = ?");
      $check_cart_numbers->execute([$pid, $user_id]);

      if($check_cart_numbers->rowCount() > 0){
         $message[] = 'Ovaj proizvod je već dodat u korpu!';
      }else{

         $check_wishlist_numbers = $conn->prepare("SELECT * FROM `wishlist` WHERE pid = ? AND user_id = ?");
         $check_wishlist_numbers->execute([$pid, $user_id]);

         if($check_wishlist_numbers->rowCount() > 0){
            $delete_wishlist = $conn->prepare("DELETE FROM `wishlist` WHERE pid = ? AND user_id = ?");
            $delete_wishlist->execute([$pid, $user_id]);
         }

         $insert_cart = $conn->prepare("INSERT INTO `cart`(user_id, pid, name, price, quantity, image) VALUES(?,?,?,?,?,?)");
         $insert_cart->execute([$user_id, $pid, $name, $price, $qty, $image]);
         $message[] = 'Proizvod je dodat u korpu!';
         
      }

   }

}

?>

How I can Force that isset to do what he must do

I tried to insert it everywhere in the code, before calling from the database, after, at the beginning, at the end... And the worst thing is when I include the wishlist_cart.php and when I press add_to_cart I get a message that it has been added to the basket, but the basket remains empty.

0 Answers
Related