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
<?php session_start(); $connect = mysqli_connect("localhost","root","","meian"); ?> ShopBlockquote
<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">
<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> </th>
<th> </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 ©<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 ‘’;
}
}
}
}
?>