E-commerce php website help!

I’m having trouble getting data from my database i think? not sure
I am creating a cart that should put data inside table when i click “add to cart” button but it seems to not work or it only calls only 1 id.
Here is my code for main index

Blockquote

<?php session_start(); $connect = mysqli_connect("localhost","root","","meian"); ?> Shop
<link href="https://fonts.googleapis.com/css?family=Poppins:200,300,400,500,600,700,800&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Lora:400,400i,700,700i&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Amatic+SC:400,700&display=swap" rel="stylesheet">

<link rel="stylesheet" href="css/open-iconic-bootstrap.min.css">
<link rel="stylesheet" href="css/animate.css">

<link rel="stylesheet" href="css/owl.carousel.min.css">
<link rel="stylesheet" href="css/owl.theme.default.min.css">
<link rel="stylesheet" href="css/magnific-popup.css">

<link rel="stylesheet" href="css/aos.css">

<link rel="stylesheet" href="css/ionicons.min.css">

<link rel="stylesheet" href="css/bootstrap-datepicker.css">
<link rel="stylesheet" href="css/jquery.timepicker.css">


<link rel="stylesheet" href="css/flaticon.css">
<link rel="stylesheet" href="css/icomoon.css">
<link rel="stylesheet" href="css/style.css">
+639 876 543 210
MEIAN Menu
      <div class="collapse navbar-collapse" id="ftco-nav">
        <ul class="navbar-nav ml-auto">
          <li class="nav-item active"><a href="index.html" class="nav-link">Home</a></li>
          <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="dropdown04" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Shop</a>
          <div class="dropdown-menu" aria-labelledby="dropdown04">
          	<a class="dropdown-item" href="shop.html">Shop</a>
            <a class="dropdown-item" href="cart.html">Cart</a>
            <a class="dropdown-item" href="checkout.html">Checkout</a>
          </div>
        </li>
          <li class="nav-item"><a href="about.html" class="nav-link">About</a></li>
          <li class="nav-item"><a href="contact.html" class="nav-link">Contact</a></li>
          <li class="nav-item cta cta-colored"><a href="cart.html" class="nav-link"><span class="icon-shopping_cart"></span>[0]</a></li>

        </ul>
      </div>
    </div>
  </nav>

<div class="hero-wrap hero-bread" style="background-image: url('images/hehi.gif');">
  <div class="container">
    <div class="row no-gutters slider-text align-items-center justify-content-center">
      <div class="col-md-9 ftco-animate text-center">
      	<p class="breadcrumbs"><span class="mr-2"><a href="index.html">Home</a></span> <span>Products</span></p>
        <h1 class="mb-0 bread">Products</h1>
      </div>
    </div>
  </div>
</div>

<section class="ftco-section">
	<div class="container">
		<div class="row justify-content-center">
			<div class="col-md-10 mb-5 text-center">
				<ul class="product-category">
					<li><a href="#" class="active">All</a></li>
					<li><a href="#">Ice-cream</a></li>
					<li><a href="#">Pastries</a></li>
					<li><a href="#">Frappé</a></li>
					<li><a href="#">Coffee</a></li>
				</ul>
			</div>
		</div>

		<div class="row">
      <?php
     $query = "SELECT * FROM product2 ORDER BY id ASC";
     $result = mysqli_query($connect, $query);
     if(mysqli_num_rows($result) > 0)
     {
       while($row = mysqli_fetch_array($result))
       {
 ?>
			<div class="col-md-6 col-lg-3 ftco-animate">
				<div class="product">

          <form method="post" action="cart.php?action=add&id=<?php echo $row["id"]; ?>">

					<class="img-prod"><img src="<?php echo $row["image"]; ?>"  class="img-fluid">
						<span class="status"></span>
						<div class="overlay"></div>

					<div class="text py-3 pb-4 px-3 text-center">
						<h3><?php echo $row["pname"]; ?></h3>
						<div class="d-flex">
							<div class="pricing">
	    						<p class="price"><span class="price-sale">P <?php echo $row["price"]; ?></span></p>
	    					</div>
    					</div>
    					<div class="bottom-area d-flex px-3">
    						<div class="m-auto d-flex">
                <input type="text" name="quantity" class="form-control" value="1" size="1">
                <input type="hidden" name="hidden_name" value="<?php echo $row["pname"]; ?>">
                <input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>">

    							<input type="submit" name="add" class="buy-now d-flex justify-content-center align-items-center mx-1" value="Add to Cart">
    								<span><i class="ion-ios-cart"></i></span>

    							</a>
							</div>
						</div>
					</div>
				</div>
			</div>
    <?php
  }
}
   ?>
		<div class="row mt-5">
      <div class="col text-center">
        <div class="block-27">
          <ul>

          </ul>
        </div>
      </div>
    </div>
	</div>
</section>

<section class="ftco-section ftco-cart">
		<div class="container">
			<div class="row">
			<div class="col-md-12 ftco-animate">
				<div class="cart-list">
    				<table class="table">
					    <thead class="thead-primary">
					      <tr class="text-center">
					        <th>&nbsp;</th>
					        <th>&nbsp;</th>
					        <th>Product name</th>
					        <th>Price</th>
					        <th>Quantity</th>
                <th>Price Detail</th>
					        <th>Order Total</th>
					      </tr>
					    </thead>

            <?php
            if(!empty($_SESSION["cart"]))
            {
              $total = 0;
              foreach($_SESSION["cart"] as $keys => $values)
              {
                 ?>

					    <tbody>
					      <tr class="text-center">
					        <td class="product-remove"><a href="cart.php?action=delete&id=<?php echo $values[product_id]; ?>"><span class="ion-ios-close"></span></a></td>

					        <td class="image-prod"><div class="img" style="<?php echo["image"]; ?>"</div></td>

					        <td class="product-name">
					        	<h3><?php echo $values["item_name"]; ?></h3>
					        </td>

					        <td class="price">P <?php echo $values["product_price"]; ?></td>

					        <td class="quantity">
				             	<?php echo $values["item_quantity"]; ?>
				          </td>

                <td class="total">P <?php echo number_format($values["item_quantity"] * $values["product_price"], 2); ?></td>


                <?php
                $total = $total * ($values["item_quantity"] * $values["product_price"]);
              }
              ?>

					        <td class="total">P <?php echo number_format($total, 2); ?></td>
					      </tr>
				          </td>

					      </tr>
              <?php
            }
             ?>
					    </tbody>
					  </table>
				  </div>
			</div>
		</div>

		</div>
		</div>
	</section>


<section class="ftco-section">
  <div class="container">
    <div class="row justify-content-center">
      <div class="col-xl-7 ftco-animate">
					<form action="#" class="billing-form">
						<h3 class="mb-4 billing-heading">Delivery</h3>
          	<div class="row align-items-end">
          		<div class="col-md-6">
                <div class="form-group">
                	<label for="firstname">Firt Name</label>
                  <input type="text" class="form-control" placeholder="">
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-group">
                	<label for="lastname">Last Name</label>
                  <input type="text" class="form-control" placeholder="">
                </div>
            </div>
            <div class="w-100"></div>
	            <div class="col-md-12">
	            	<div class="form-group">
	            		<label for="country">State / Country</label>
	            		<div class="select-wrap">
	                  <div class="icon"><span class="ion-ios-arrow-down"></span></div>
	                  <select name="" id="" class="form-control">
	                  	<option value="">France</option>
	                    <option value="">Italy</option>
	                    <option value="">Philippines</option>
	                    <option value="">South Korea</option>
	                    <option value="">Hongkong</option>
	                    <option value="">Japan</option>
	                  </select>
	                </div>
	            	</div>
	            </div>
	            <div class="w-100"></div>
	            <div class="col-md-6">
	            	<div class="form-group">
                	<label for="streetaddress">Street Address</label>
                  <input type="text" class="form-control" placeholder="House number and street name">
                </div>
	            </div>
	            <div class="col-md-6">
	            	<div class="form-group">
                  <input type="text" class="form-control" placeholder="Appartment, suite, unit etc: (optional)">
                </div>
	            </div>
	            <div class="w-100"></div>
	            <div class="col-md-6">
	            	<div class="form-group">
                	<label for="towncity">Town / City</label>
                  <input type="text" class="form-control" placeholder="">
                </div>
	            </div>
	            <div class="col-md-6">
	            	<div class="form-group">
	            		<label for="postcodezip">Postcode / ZIP *</label>
                  <input type="text" class="form-control" placeholder="">
                </div>
	            </div>
	            <div class="w-100"></div>
	            <div class="col-md-6">
                <div class="form-group">
                	<label for="phone">Phone</label>
                  <input type="text" class="form-control" placeholder="">
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-group">
                	<label for="emailaddress">Email Address</label>
                  <input type="text" class="form-control" placeholder="">
                </div>
            </div>
            <div class="w-100"></div>
            <div class="col-md-12">
            	<div class="form-group mt-4">
								</div>
            </div>
            </div>
          </form><!-- END -->
				</div>
				<div class="col-xl-5">
          <div class="row mt-5 pt-3">
          	<div class="col-md-12 d-flex mb-5">
          		<div class="cart-detail cart-total p-3 p-md-4">
          			<h3 class="billing-heading mb-4">Cart Total</h3>
          			<p class="d-flex">
	    						<span>Subtotal</span>
	    						<span>$20.60</span>
	    					</p>
	    					<p class="d-flex">
	    						<span>Delivery</span>
	    						<span>$0.00</span>
	    					</p>
	    					<p class="d-flex">
	    						<span>Discount</span>
	    						<span>$3.00</span>
	    					</p>
	    					<hr>
	    					<p class="d-flex total-price">
	    						<span>Total</span>
	    						<span>$17.60</span>
	    					</p>
							</div>
          	</div>

								<p><a href="#"class="btn btn-primary py-3 px-4">Place an order</a></p>

          	</div>
          </div>
      </div>
    </div>
  </div>
</section>

<footer class="ftco-footer ftco-section">
  <div class="container">
  	<div class="row">
  		<div class="mouse">
					<a href="#" class="mouse-icon">
						<div class="mouse-wheel"><span class="ion-ios-arrow-up"></span></div>
					</a>
				</div>
  	</div>


        <p>
					  Copyright &copy;<script>document.write(new Date().getFullYear());</script> All rights reserved
					</p>
      </div>
    </div>
  </div>
</footer>
<?php

session_start();
$connect = mysqli_connect(“localhost”,“root”,"",“meian”);
if(isset($_POST[“add”]))
{
if(isset($_SESSION[“cart”]))
{
$item_array_id = array_column($_SESSION[“cart”], “product_id”);
if(!in_array($_GET[“id”], $item_array_id))
{
$count = count($_SESSION[“cart”]);
$item_array = array(
‘product_id’ => $_GET[“id”],
‘item_name’ => $_POST[“hidden_name”],
‘product_price’ => $_POST[“hidden_price”],
‘item_quantity’ => $_POST[“quantity”]
);
$_SESSION[“cart”][$count] = $item_array;
echo ‘’;
}
echo ‘’;
echo ‘’;
}
else
{
$item_array = array(
‘product_id’ => $_GET[“id”],
‘item_name’ => $_POST[“hidden_name”],
‘product_price’ => $_POST[“hidden_price”],
‘item_quantity’ => $_POST[“quantity”]
);
$_SESSION[“cart”][0] = $item_array;
}
}
if(isset($_GET[“action”]))
{
if($_GET[“action”] == “delete”)
{
foreach($_SESSION[“cart”] as $keys => $values)
{
if($values[“product_id”] == $_GET[“id”])
{
unset($_SESSION[“cart”][$keys]);
echo ‘’;
echo ‘’;
}
}
}
}

?>

Cannot really tell what’s wrong with the code since the forum software mangled it. If you edit your post above, you can add bbcode [ code ] [ /code ] tags (without the spaces).

However, your session cart definition is overly complicated, repetitive, and insecure. The add to cart form should only submit the add action, item id, and the quantity, and these are the only things that should be stored in the session cart, with the item id as the cart’s array index, and the quantity as the corresponding array value. By passing other item information through the form, especially the price, the code is open to anyone setting the price to any value. You should also be consistent and use just $_POST data. Don’t mix $_GET and $_POST data. You are apparently also planning on using a link to delete an item, use a post method form for the delete action too.

Once the session cart is changed to use the item ids as the cart’s array index, you can directly test, reference, set, or unset an item in the cart without needing any loop or complicated logic. To display the contents of the cart, you would get all the item ids at once (see array_keys()), query to get all the cart item information from the database table, loop over the data from the query to display the cart, getting the corresponding quantity from the session cart using the item id to directly reference the correct quantity.

I tried to fix what i can but now my problem is when i try to add an item to the cart it stills only get the latest id from my database and also if i try to remove an item from cart it removes all of it
Also when i try to make the delete to $_POST it just sends me to another webpage with no id

<?php
session_start();
$connect = mysqli_connect("localhost","root","","meian");
?>



<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Shop</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link href="https://fonts.googleapis.com/css?family=Poppins:200,300,400,500,600,700,800&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Lora:400,400i,700,700i&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Amatic+SC:400,700&display=swap" rel="stylesheet">

    <link rel="stylesheet" href="css/open-iconic-bootstrap.min.css">
    <link rel="stylesheet" href="css/animate.css">

    <link rel="stylesheet" href="css/owl.carousel.min.css">
    <link rel="stylesheet" href="css/owl.theme.default.min.css">
    <link rel="stylesheet" href="css/magnific-popup.css">

    <link rel="stylesheet" href="css/aos.css">

    <link rel="stylesheet" href="css/ionicons.min.css">

    <link rel="stylesheet" href="css/bootstrap-datepicker.css">
    <link rel="stylesheet" href="css/jquery.timepicker.css">


    <link rel="stylesheet" href="css/flaticon.css">
    <link rel="stylesheet" href="css/icomoon.css">
    <link rel="stylesheet" href="css/style.css">
  </head>
   <body class="goto-here">
		<div class="py-1 bg-primary">
    	<div class="container">
    		<div class="row no-gutters d-flex align-items-start align-items-center px-md-0">
	    		<div class="col-lg-12 d-block">
		    		<div class="row d-flex">
		    			<div class="col-md pr-4 d-flex topper align-items-center">
					    	<div class="icon mr-2 d-flex justify-content-center align-items-center"><span class="icon-phone2"></span></div>
						    <span class="text">+639 876 543 210</span>
					    </div>
					    <div class="col-md pr-4 d-flex topper align-items-center">
					    	<div class="icon mr-2 d-flex justify-content-center align-items-center"><span class="icon-paper-plane"></span></div>
						    <span class="text">[email protected]</span>
					    </div>
					    <div class="col-md-5 pr-4 d-flex topper align-items-center text-lg-right">
						    <span class="text"></span>
					    </div>
				    </div>
			    </div>
		    </div>
		  </div>
    </div>
    <nav class="navbar navbar-expand-lg navbar-dark ftco_navbar bg-dark ftco-navbar-light" id="ftco-navbar">
	    <div class="container">
	      <a class="navbar-brand" href="index.html">MEIAN</a>
	      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#ftco-nav" aria-controls="ftco-nav" aria-expanded="false" aria-label="Toggle navigation">
	        <span class="oi oi-menu"></span> Menu
	      </button>

	      <div class="collapse navbar-collapse" id="ftco-nav">
	        <ul class="navbar-nav ml-auto">
	          <li class="nav-item active"><a href="index.html" class="nav-link">Home</a></li>
	          <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" href="#" id="dropdown04" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Shop</a>
              <div class="dropdown-menu" aria-labelledby="dropdown04">
              	<a class="dropdown-item" href="shop.html">Shop</a>
                <a class="dropdown-item" href="cart.html">Cart</a>
                <a class="dropdown-item" href="checkout.html">Checkout</a>
              </div>
            </li>
	          <li class="nav-item"><a href="about.html" class="nav-link">About</a></li>
	          <li class="nav-item"><a href="contact.html" class="nav-link">Contact</a></li>
	          <li class="nav-item cta cta-colored"><a href="cart.html" class="nav-link"><span class="icon-shopping_cart"></span>[0]</a></li>

	        </ul>
	      </div>
	    </div>
	  </nav>

    <div class="hero-wrap hero-bread" style="background-image: url('images/hehi.gif');">
      <div class="container">
        <div class="row no-gutters slider-text align-items-center justify-content-center">
          <div class="col-md-9 ftco-animate text-center">
          	<p class="breadcrumbs"><span class="mr-2"><a href="index.html">Home</a></span> <span>Products</span></p>
            <h1 class="mb-0 bread">Products</h1>
          </div>
        </div>
      </div>
    </div>

    <section class="ftco-section">
    	<div class="container">
    		<div class="row justify-content-center">
    			<div class="col-md-10 mb-5 text-center">
    				<ul class="product-category">
    					<li><a href="#" class="active">All</a></li>
    					<li><a href="#">Ice-cream</a></li>
    					<li><a href="#">Pastries</a></li>
    					<li><a href="#">Frappé</a></li>
    					<li><a href="#">Coffee</a></li>
    				</ul>
    			</div>
    		</div>

    		<div class="row">
          <?php
         $query = "SELECT * FROM product2 ORDER BY id ASC";
         $result = mysqli_query($connect, $query);
         if(mysqli_num_rows($result) > 0)
         {
           while($row = mysqli_fetch_array($result))
           {
     ?>
    			<div class="col-md-6 col-lg-3 ftco-animate">
    				<div class="product">

              <form method="post" action="cart.php?action=add&id=<?php echo $row["id"]; ?>">

    					<class="img-prod"><img src="<?php echo $row["image"]; ?>"  class="img-fluid">
    						<span class="status"></span>
    						<div class="overlay"></div>

    					<div class="text py-3 pb-4 px-3 text-center">
    						<h3><?php echo $row["pname"]; ?></h3>
    						<div class="d-flex">
    							<div class="pricing">
		    						<p class="price"><span class="price-sale">P <?php echo $row["price"]; ?></span></p>
		    					</div>
	    					</div>
	    					<div class="bottom-area d-flex px-3">
	    						<div class="m-auto d-flex">
                    <input type="text" align="center" name="quantity" class="form-control" value="1" size="1">
                    <input type="hidden" name="hidden_name" value="<?php echo $row["pname"]; ?>">
                    <input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>">

	    							<input type="submit" name="add"  value="Add to Cart">


    							</div>
    						</div>
    					</div>
    				</div>
    			</div>
        <?php
      }
    }
       ?>

    	</div>
    </section>

	<section class="ftco-section ftco-cart">
				<div class="row">
    			<div class="col-md-12 ftco-animate">
    				<div class="cart-list">
	    				<table class="table">
						    <thead class="thead-primary">
						      <tr class="text-center">
						        <th>&nbsp;</th>
						        <th>&nbsp;</th>
						        <th>Product name</th>
						        <th>Price</th>
						        <th>Quantity</th>
                    <th>Price Detail</th>
						        <th>Order Total</th>
						      </tr>
						    </thead>

                <?php
                if(!empty($_SESSION["cart"]))
                {
                  $total = 0;
                  foreach($_SESSION["cart"] as $keys => $values)
                  {
                     ?>

						    <tbody>
						      <tr class="text-center">
						        <td class="product-remove"><a href="cart.php?action=delete&id=<?php echo $values["product_id"]; ?>"><span class="ion-ios-close"></span></a></td>

                  <td><img src="<?php echo $values["image"]; ?>" div class="img"></div></td>

						        <td class="product-name">
						        	<h3><?php echo $values["item_name"]; ?></h3>
						        </td>

						        <td class="price">P <?php echo $values["product_price"]; ?></td>

						        <td class="quantity">
					             	<?php echo $values["item_quantity"]; ?>
					          </td>

                    <td class="total">P <?php echo number_format($values["item_quantity"] * $values["product_price"], 2); ?></td>


                    <?php
                    $total = $total * ($values["item_quantity"] * $values["product_price"]);
                  }
                  ?>

						        <td class="total">P <?php echo number_format($total, 2); ?></td>
						      </tr>
					          </td>

						      </tr>
                  <?php
                }
                 ?>
						    </tbody>
						  </table>
					  </div>
    			</div>
    		</div>

    		</div>
			</div>
		</section>


    <section class="ftco-section">
      <div class="container">
        <div class="row justify-content-center">
          <div class="col-xl-7 ftco-animate">
						<form action="#" class="billing-form">
							<h3 class="mb-4 billing-heading">Delivery</h3>
	          	<div class="row align-items-end">
	          		<div class="col-md-6">
	                <div class="form-group">
	                	<label for="firstname">Firt Name</label>
	                  <input type="text" class="form-control" placeholder="">
	                </div>
	              </div>
	              <div class="col-md-6">
	                <div class="form-group">
	                	<label for="lastname">Last Name</label>
	                  <input type="text" class="form-control" placeholder="">
	                </div>
                </div>
                <div class="w-100"></div>
		            <div class="col-md-12">
		            	<div class="form-group">
		            		<label for="country">State / Country</label>
		            		<div class="select-wrap">
		                  <div class="icon"><span class="ion-ios-arrow-down"></span></div>
		                  <select name="" id="" class="form-control">
		                  	<option value="">France</option>
		                    <option value="">Italy</option>
		                    <option value="">Philippines</option>
		                    <option value="">South Korea</option>
		                    <option value="">Hongkong</option>
		                    <option value="">Japan</option>
		                  </select>
		                </div>
		            	</div>
		            </div>
		            <div class="w-100"></div>
		            <div class="col-md-6">
		            	<div class="form-group">
	                	<label for="streetaddress">Street Address</label>
	                  <input type="text" class="form-control" placeholder="House number and street name">
	                </div>
		            </div>
		            <div class="col-md-6">
		            	<div class="form-group">
	                  <input type="text" class="form-control" placeholder="Appartment, suite, unit etc: (optional)">
	                </div>
		            </div>
		            <div class="w-100"></div>
		            <div class="col-md-6">
		            	<div class="form-group">
	                	<label for="towncity">Town / City</label>
	                  <input type="text" class="form-control" placeholder="">
	                </div>
		            </div>
		            <div class="col-md-6">
		            	<div class="form-group">
		            		<label for="postcodezip">Postcode / ZIP *</label>
	                  <input type="text" class="form-control" placeholder="">
	                </div>
		            </div>
		            <div class="w-100"></div>
		            <div class="col-md-6">
	                <div class="form-group">
	                	<label for="phone">Phone</label>
	                  <input type="text" class="form-control" placeholder="">
	                </div>
	              </div>
	              <div class="col-md-6">
	                <div class="form-group">
	                	<label for="emailaddress">Email Address</label>
	                  <input type="text" class="form-control" placeholder="">
	                </div>
                </div>
                <div class="w-100"></div>
                <div class="col-md-12">
                	<div class="form-group mt-4">
									</div>
                </div>
	            </div>
	          </form><!-- END -->
					</div>
					<div class="col-xl-5">
	          <div class="row mt-5 pt-3">
	          	<div class="col-md-12 d-flex mb-5">
	          		<div class="cart-detail cart-total p-3 p-md-4">
	          			<h3 class="billing-heading mb-4">Cart Total</h3>
	          			<p class="d-flex">
		    						<span>Subtotal</span>
		    						<span>$20.60</span>
		    					</p>
		    					<p class="d-flex">
		    						<span>Delivery</span>
		    						<span>$0.00</span>
		    					</p>
		    					<p class="d-flex">
		    						<span>Discount</span>
		    						<span>$3.00</span>
		    					</p>
		    					<hr>
		    					<p class="d-flex total-price">
		    						<span>Total</span>
		    						<span>$17.60</span>
		    					</p>
								</div>
	          	</div>

									<p><a href="#"class="btn btn-primary py-3 px-4">Place an order</a></p>

	          	</div>
	          </div>
          </div>
        </div>
      </div>
    </section>

    <footer class="ftco-footer ftco-section">
      <div class="container">
      	<div class="row">
      		<div class="mouse">
						<a href="#" class="mouse-icon">
							<div class="mouse-wheel"><span class="ion-ios-arrow-up"></span></div>
						</a>
					</div>
      	</div>


            <p>
						  Copyright &copy;<script>document.write(new Date().getFullYear());</script> All rights reserved
						</p>
          </div>
        </div>
      </div>
    </footer>



  <!-- loader -->
  <div id="ftco-loader" class="show fullscreen"><svg class="circular" width="48px" height="48px"><circle class="path-bg" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke="#eeeeee"/><circle class="path" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke-miterlimit="10" stroke="#F96D00"/></svg></div>


  <script src="js/jquery.min.js"></script>
  <script src="js/jquery-migrate-3.0.1.min.js"></script>
  <script src="js/popper.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
  <script src="js/jquery.easing.1.3.js"></script>
  <script src="js/jquery.waypoints.min.js"></script>
  <script src="js/jquery.stellar.min.js"></script>
  <script src="js/owl.carousel.min.js"></script>
  <script src="js/jquery.magnific-popup.min.js"></script>
  <script src="js/aos.js"></script>
  <script src="js/jquery.animateNumber.min.js"></script>
  <script src="js/bootstrap-datepicker.js"></script>
  <script src="js/scrollax.min.js"></script>
  <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBVWaKrjvy3MaE7SQ74_uJiULgl1JY0H2s&sensor=false"></script>
  <script src="js/google-map.js"></script>
  <script src="js/main.js"></script>

  </body>
</html>
<?php
session_start();
$connect = mysqli_connect("localhost","root","","meian");
if(isset($_POST["add"]))
{
	if(isset($_SESSION["cart"]))
	{
		$item_array_id = array_column($_SESSION["cart"], "product_id");
		if(!in_array($_GET["id"], $item_array_id))
		{
			$count = count($_SESSION["cart"]);
			$item_array = array(
				'product_id' => $_POST["id"],
				'item_name' => $_POST["hidden_name"],
				'product_price' => $_POST["hidden_price"],
				'item_quantity' => $_POST["quantity"]
			);
			$_SESSION["cart"][$count] = $item_array;
			echo '<script>window.location="shop.php"</script>';
		}
			echo '<script>alert("Product already added")</script>';
			echo '<script>window.location="shop.php"</script>';
	}
	else
		{
			$item_array = array(
				'product_id' => $_POST["id"],
				'item_name' => $_POST["hidden_name"],
				'product_price' => $_POST["hidden_price"],
				'item_quantity' => $_POST["quantity"]
			);
			$_SESSION["cart"][0] = $item_array;
		}
	}
	if(isset($_POST["action"]))
	{
		if($_GET["action"] == "delete")
		{
			foreach($_SESSION["cart"] as $keys => $values)
			{
				if($values["product_id"] == $_POST["id"])
				{
					unset($_SESSION["cart"][$keys]);
					echo '<script>alert("Product has been removed")</script>';
					echo '<script>window.location="shop.php"</script>';
				}
			}
		}
	}

 ?>

My problem is when I try to add a product into my cart it only gets 1 type of id always, and quantity doesnt work also. and if i try to add another product into cart it doesnt add

Heres my code

<?php
session_start();
$connect = mysqli_connect("localhost", "root", "", "meian");

$cart = []; // assign an empty array to $cart by default
if(isset($_SESSION["cart"])) { // IF there is cart data in the session THEN ...
    $cart = $_SESSION["cart"]; // load complete cart data from session
}
// From here we know for sure that we have an existing $cart variable and that it is an array!

$product_id = 0;
if(isset($_GET["id"])) {
    $product_id =  (int) $_GET["id"]; // id of the product to add or delete
}
// From here we know for sure that we have an existing $product_id variable and that it is a integer!

// add or update a product to the cart
if(isset($_POST["add"]))
{
    // now get the trustable data like prices from the database and not from the POST array
    $result = mysqli_query($connect, "SELECT id, pname, image, price FROM product2 WHERE id=1" . $product_id);
    if($result === false) {
        die('query is not valid');
    }

    $product = mysqli_fetch_assoc($result); // the product data that we need
    if($product === null) {
        die('no product found with id ' . $product_id);
    }

    // add or update product to cart
    $cart[$product_id] = array(
            'product_id' => $product_id,
            'item_name' => $product["pname"], // get name from the database so that users cannot mess with it
            'product_price' => $product["price"],   // get price from the database so that users cannot mess with the prices either
            'item_quantity' => $_POST["quantity"]
    );

    // save cart to session
    $_SESSION['cart'] = $cart;

    // redirect
    header('Location: shop.php');
    exit;
}

// remove a product from the cart
if(isset($_GET["action"]) && $_GET["action"] == "delete")
{
    unset($cart[$product_id]);

    // save cart to session
    $_SESSION['cart'] = $cart;

    // redirect
    header('Location: shop.php');
    exit;
}

?>



<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Shop</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link href="https://fonts.googleapis.com/css?family=Poppins:200,300,400,500,600,700,800&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Lora:400,400i,700,700i&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Amatic+SC:400,700&display=swap" rel="stylesheet">

    <link rel="stylesheet" href="css/open-iconic-bootstrap.min.css">
    <link rel="stylesheet" href="css/animate.css">

    <link rel="stylesheet" href="css/owl.carousel.min.css">
    <link rel="stylesheet" href="css/owl.theme.default.min.css">
    <link rel="stylesheet" href="css/magnific-popup.css">

    <link rel="stylesheet" href="css/aos.css">

    <link rel="stylesheet" href="css/ionicons.min.css">

    <link rel="stylesheet" href="css/bootstrap-datepicker.css">
    <link rel="stylesheet" href="css/jquery.timepicker.css">


    <link rel="stylesheet" href="css/flaticon.css">
    <link rel="stylesheet" href="css/icomoon.css">
    <link rel="stylesheet" href="css/style.css">
  </head>
   <body class="goto-here">
		<div class="py-1 bg-primary">
    	<div class="container">
    		<div class="row no-gutters d-flex align-items-start align-items-center px-md-0">
	    		<div class="col-lg-12 d-block">
		    		<div class="row d-flex">
		    			<div class="col-md pr-4 d-flex topper align-items-center">
					    	<div class="icon mr-2 d-flex justify-content-center align-items-center"><span class="icon-phone2"></span></div>
						    <span class="text">+639 876 543 210</span>
					    </div>
					    <div class="col-md pr-4 d-flex topper align-items-center">
					    	<div class="icon mr-2 d-flex justify-content-center align-items-center"><span class="icon-paper-plane"></span></div>
						    <span class="text">[email protected]</span>
					    </div>
					    <div class="col-md-5 pr-4 d-flex topper align-items-center text-lg-right">
						    <span class="text"></span>
					    </div>
				    </div>
			    </div>
		    </div>
		  </div>
    </div>
    <nav class="navbar navbar-expand-lg navbar-dark ftco_navbar bg-dark ftco-navbar-light" id="ftco-navbar">
	    <div class="container">
	      <a class="navbar-brand" href="index.html">MEIAN</a>
	      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#ftco-nav" aria-controls="ftco-nav" aria-expanded="false" aria-label="Toggle navigation">
	        <span class="oi oi-menu"></span> Menu
	      </button>

	      <div class="collapse navbar-collapse" id="ftco-nav">
	        <ul class="navbar-nav ml-auto">
	          <li class="nav-item active"><a href="index.html" class="nav-link">Home</a></li>
	          <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" href="#" id="dropdown04" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Shop</a>
              <div class="dropdown-menu" aria-labelledby="dropdown04">
              	<a class="dropdown-item" href="shop.html">Shop</a>
                <a class="dropdown-item" href="cart.html">Cart</a>
                <a class="dropdown-item" href="checkout.html">Checkout</a>
              </div>
            </li>
	          <li class="nav-item"><a href="about.html" class="nav-link">About</a></li>
	          <li class="nav-item"><a href="contact.html" class="nav-link">Contact</a></li>
	          <li class="nav-item cta cta-colored"><a href="cart.html" class="nav-link"><span class="icon-shopping_cart"></span>[0]</a></li>

	        </ul>
	      </div>
	    </div>
	  </nav>

    <div class="hero-wrap hero-bread" style="background-image: url('images/hehi.gif');">
      <div class="container">
        <div class="row no-gutters slider-text align-items-center justify-content-center">
          <div class="col-md-9 ftco-animate text-center">
          	<p class="breadcrumbs"><span class="mr-2"><a href="index.html">Home</a></span> <span>Products</span></p>
            <h1 class="mb-0 bread">Products</h1>
          </div>
        </div>
      </div>
    </div>

    <section class="ftco-section">
    	<div class="container">
    		<div class="row justify-content-center">
    			<div class="col-md-10 mb-5 text-center">
    				<ul class="product-category">
    					<li><a href="#" class="active">All</a></li>
    					<li><a href="#">Ice-cream</a></li>
    					<li><a href="#">Pastries</a></li>
    					<li><a href="#">Frappé</a></li>
    					<li><a href="#">Coffee</a></li>
    				</ul>
    			</div>
    		</div>

    		<div class="row">
          <?php
         $query = "SELECT * FROM product2 ORDER BY id ASC";
         $result = mysqli_query($connect, $query);
         if(mysqli_num_rows($result) > 0)
         {
           while($row = mysqli_fetch_array($result))
           {
     ?>
    			<div class="col-md-6 col-lg-3 ftco-animate">
    				<div class="product">

              <form method="post" action="shop.php?action=add&id=<?php echo $row["id"]; ?>">

    					<class="img-prod"><img src="<?php echo $row["image"]; ?>"  class="img-fluid">
    						<span class="status"></span>
    						<div class="overlay"></div>

    					<div class="text py-3 pb-4 px-3 text-center">
    						<h3><?php echo $row["pname"]; ?></h3>
    						<div class="d-flex">
    							<div class="pricing">
		    						<p class="price"><span class="price-sale">P <?php echo $row["price"]; ?></span></p>
		    					</div>
	    					</div>
	    					<div class="bottom-area d-flex px-3">
	    						<div class="m-auto d-flex">
                    <input type="text" align="center" name="quantity" class="form-control" value="1" size="1">
                    <input type="hidden" name="hidden_name" value="<?php echo $row["pname"]; ?>">
                    <input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>">

	    							<input type="submit" name="add"  value="Add to Cart">


    							</div>
    						</div>
    					</div>
    				</div>
    			</div>
        <?php
      }
    }
       ?>

    	</div>
    </section>

	<section class="ftco-section ftco-cart">
				<div class="row">
    			<div class="col-md-12 ftco-animate">
    				<div class="cart-list">
	    				<table class="table">
						    <thead class="thead-primary">
						      <tr class="text-center">
						        <th>&nbsp;</th>
						        <th>&nbsp;</th>
						        <th>Product name</th>
						        <th>Price</th>
						        <th>Quantity</th>
                    <th>Price Detail</th>
                    <th>&nbsp;</th>

						      </tr>
						    </thead>

                <?php
                if(!empty($_SESSION["cart"]))
                {
                  $total = 0;
                  foreach($_SESSION["cart"] as $keys => $values)
                  {
                     ?>

						    <tbody>
						      <tr class="text-center">
						        <td class="product-remove"><a href="shop.php?action=delete&id=<?php echo $values["product_id"]; ?>"><span class="ion-ios-close"></span></a></td>

                  <td class="image-prod"><div class="img" style="background-image:url(images/product-3.jpg);"></div></td>

						        <td class="product-name">
						        	<h3><?php echo $values["item_name"]; ?></h3>
						        </td>

						        <td class="price">P <?php echo $values["product_price"]; ?></td>

						        <td class="quantity">
					             	<?php echo $values["item_quantity"]; ?>
					          </td>

                    <td class="total">P <?php echo number_format($values["item_quantity"] * $values["product_price"], 2); ?></td>





						      </tr>
                  <?php
                }
                 ?>

						    </tbody>
						  </table>
					  </div>
    			</div>

          <?php
          $total = $total * ($values["item_quantity"] * $values["product_price"]);
          }
          ?>



              <div class="col-md-12 d-flex mb-5">
                <div class="cart-detail cart-total p-3 p-md-4">
                  <h3 class="billing-heading mb-4">Cart Total</h3>
                  <hr>
                  <p class="d-flex total-price">
                    <span>Total</span>
                    <span>P <?php echo number_format($total, 2); ?></span>
                  </p>
                </div>
              </div>

    		</div>

    		</div>
			</div>

		</section>


    <section class="ftco-section">
      <div class="container">
        <div class="row justify-content-center">
          <div class="col-xl-7 ftco-animate">
						<form action="#" class="billing-form">
							<h3 class="mb-4 billing-heading">Delivery</h3>
	          	<div class="row align-items-end">
	          		<div class="col-md-6">
	                <div class="form-group">
	                	<label for="firstname">Firt Name</label>
	                  <input type="text" class="form-control" placeholder="">
	                </div>
	              </div>
	              <div class="col-md-6">
	                <div class="form-group">
	                	<label for="lastname">Last Name</label>
	                  <input type="text" class="form-control" placeholder="">
	                </div>
                </div>
                <div class="w-100"></div>
		            <div class="col-md-12">
		            	<div class="form-group">
		            		<label for="country">State / Country</label>
		            		<div class="select-wrap">
		                  <div class="icon"><span class="ion-ios-arrow-down"></span></div>
		                  <select name="" id="" class="form-control">
		                  	<option value="">France</option>
		                    <option value="">Italy</option>
		                    <option value="">Philippines</option>
		                    <option value="">South Korea</option>
		                    <option value="">Hongkong</option>
		                    <option value="">Japan</option>
		                  </select>
		                </div>
		            	</div>
		            </div>
		            <div class="w-100"></div>
		            <div class="col-md-6">
		            	<div class="form-group">
	                	<label for="streetaddress">Street Address</label>
	                  <input type="text" class="form-control" placeholder="House number and street name">
	                </div>
		            </div>
		            <div class="col-md-6">
		            	<div class="form-group">
	                  <input type="text" class="form-control" placeholder="Appartment, suite, unit etc: (optional)">
	                </div>
		            </div>
		            <div class="w-100"></div>
		            <div class="col-md-6">
		            	<div class="form-group">
	                	<label for="towncity">Town / City</label>
	                  <input type="text" class="form-control" placeholder="">
	                </div>
		            </div>
		            <div class="col-md-6">
		            	<div class="form-group">
		            		<label for="postcodezip">Postcode / ZIP *</label>
	                  <input type="text" class="form-control" placeholder="">
	                </div>
		            </div>
		            <div class="w-100"></div>
		            <div class="col-md-6">
	                <div class="form-group">
	                	<label for="phone">Phone</label>
	                  <input type="text" class="form-control" placeholder="">
	                </div>
	              </div>
	              <div class="col-md-6">
	                <div class="form-group">
	                	<label for="emailaddress">Email Address</label>
	                  <input type="text" class="form-control" placeholder="">
	                </div>
                </div>
                <div class="w-100"></div>
                <div class="col-md-12">
                	<div class="form-group mt-4">
									</div>
                </div>
	            </div>
	          </form>
					</div>
					<div class="col-xl-5">
	          <div class="row mt-5 pt-3">
	          	<div class="col-md-12 d-flex mb-5">
	          		<div class="cart-detail cart-total p-3 p-md-4">
	          			<h3 class="billing-heading mb-4">Cart Total</h3>
		    					<hr>
		    					<p class="d-flex total-price">
		    						<span>Total</span>
		    						<span>$17.60</span>
		    					</p>
								</div>
	          	</div>

									<p><a href="#"class="btn btn-primary py-3 px-4">Place an order</a></p>

	          	</div>
	          </div>
          </div>
        </div>
      </div>
    </section>

    <footer class="ftco-footer ftco-section">
      <div class="container">
      	<div class="row">
      		<div class="mouse">
						<a href="#" class="mouse-icon">
							<div class="mouse-wheel"><span class="ion-ios-arrow-up"></span></div>
						</a>
					</div>
      	</div>


            <p>
						  Copyright &copy;<script>document.write(new Date().getFullYear());</script> All rights reserved
						</p>
          </div>
        </div>
      </div>
    </footer>



  <!-- loader -->
  <div id="ftco-loader" class="show fullscreen"><svg class="circular" width="48px" height="48px"><circle class="path-bg" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke="#eeeeee"/><circle class="path" cx="24" cy="24" r="22" fill="none" stroke-width="4" stroke-miterlimit="10" stroke="#F96D00"/></svg></div>


  <script src="js/jquery.min.js"></script>
  <script src="js/jquery-migrate-3.0.1.min.js"></script>
  <script src="js/popper.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
  <script src="js/jquery.easing.1.3.js"></script>
  <script src="js/jquery.waypoints.min.js"></script>
  <script src="js/jquery.stellar.min.js"></script>
  <script src="js/owl.carousel.min.js"></script>
  <script src="js/jquery.magnific-popup.min.js"></script>
  <script src="js/aos.js"></script>
  <script src="js/jquery.animateNumber.min.js"></script>
  <script src="js/bootstrap-datepicker.js"></script>
  <script src="js/scrollax.min.js"></script>
  <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBVWaKrjvy3MaE7SQ74_uJiULgl1JY0H2s&sensor=false"></script>
  <script src="js/google-map.js"></script>
  <script src="js/main.js"></script>

  </body>
</html>

So whats the exact line the error occurs? What debugging info did you obtain while outputting every involved variable with var_dump()?

Sponsor our Newsletter | Privacy Policy | Terms of Service