php select option from dropdown - php

I am working on a project I need to select option from html dropdown list and then display the data selected from database. Just like filter and display the filtered data from database.
Still not working
Am getting error:
Its just displaying all data from db not by filtered query
<form action="search.php" method="POST">
<div class="md-form">
<select class="mdb-select" name="state_search" required>
<option disabled selected>Choose your State</option>
<option name="state" value="Abia State">Abia State</option>
<option name="state" value="Adamawa State">Adamawa State</option>
<option name="state" value="Anambra State">Anambra State</option>
</select>
</div>
<div class="col-lg-4">
<div class="md-form">
<select class="mdb-select" name="school_search">
<option value="" disabled selected>Choose your School</option>
<option value="Michael Opkara University">Mouau</option>
<option value="University of Benin">UniBen</option>
<option value="University of Porthacourt">UniPort</option>
</select>
</div>
<button type="submit" class="btn purple-gradient btn-lg" name="filter">Search <i class="fa fa-paper-plane-o ml-1"></i></button>
</div>
</form>
PHP
<?php
$school_search = $connect->real_escape_string($_POST["school_search"]);
$state_search = $connect->real_escape_string($_POST["state_search"]);
$query = mysqli_query($connect, "SELECT * FROM roomate WHERE (`state` LIKE '%".$state_search."%') OR (`school` LIKE '%".$school_search."%')") or die(mysql_error());
if($query){
while($row = mysqli_fetch_array($query)){
?>
<div class="container" style="padding-top:70px;">
<!--Grid row-->
<div class="row">
<?php
foreach ($query as $user) {?>
<div class="col-lg-4 col-md-12 mb-r">
<!--Featured image-->
<div class="overlay hm-white-slight z-depth-1-half mb-2">
<img src="<?php echo $user['room_avatar']?>" class="img-fluid" alt="avatar image">
<a><div class="mask"></div></a>
</div>
<!--Excerpt-->
<a href="" class="pink-text">
<h6>
<i class="fa fa-map"></i><strong><?php echo $user['city'];?>, <?php echo $user['state'];?></strong><br>
<small><?php echo $user['school'];?></small>
</h6>
</a>
<h4><?php echo $user['type'];?></h4>
<p>
by <a><strong><?php echo $user['room_name'];?></strong></a>
<?php echo $user['created'];?>
</p>
<label class="badge badge-danger"> non-verified </label>
<a href="contact.php">
<label class="badge badge-primary">Report Scam</label>
</a>
<p><?php echo $user['discription'];?></p>
<a class="btn btn-pink btn-rounded">Contact <?php echo $user['room_name'];?></a>
</div>
<?php
} ?>
</div>
</div>
<?php
}
}
else{
echo(mysql_error());
}
?>
Need help making this work out

The name of your select needs to match the $_POST variable key.
So in your case your <select> need the name state_search.
or change your $_POST search to $_POST['state'] to match your select name.
also you should use the $state_search variable in your query instead of $query.
You may have more issues , just pay attention to details.

You put to much HTML in echo function.
<?php
$school_search = $connect->real_escape_string($_POST["school_search"]);
$state_search = $connect->real_escape_string($_POST["state_search"]);
$query = mysqli_query($connect, "SELECT * FROM roomate WHERE (`room_name` LIKE '%".$query."%') OR (`discription` LIKE '%".$query."%') OR (`discription` LIKE '%".$query."%') OR (`type` LIKE '%".$query."%')") or die(mysql_error());;
$result = mysqli_query($connect, $query);
if($result){
while($row = mysqli_fetch_array($result)){
?>
<div class="container" style="padding-top:70px;">
<!--Grid row-->
<div class="row">
<?php
foreach ($query as $user) {?>
<div class="col-lg-4 col-md-12 mb-r">
<!--Featured image-->
<div class="overlay hm-white-slight z-depth-1-half mb-2">
<img src="<?php echo $user['room_avatar']?>" class="img-fluid" alt="avatar image">
<a><div class="mask"></div></a>
</div>
<!--Excerpt-->
<a href="" class="pink-text">
<h6>
<i class="fa fa-map"></i><strong><?php echo $user['city'];?>, <?php echo $user['state'];?></strong><br>
<small><?php echo $user['school'];?></small>
</h6>
</a>
<h4><?php echo $user['type'];?></h4>
<p>
by <a><strong><?php echo $user['room_name'];?></strong></a>
<?php echo $user['created'];?>
</p>
<label class="badge badge-danger"> non-verified </label>
<a href="contact.php">
<label class="badge badge-primary">Report Scam</label>
</a>
<p><?php echo $user['discription'];?></p>
<a class="btn btn-pink btn-rounded">Contact <?php echo $user['room_name'];?></a>
</div>
<?php
} ?>
</div>
</div>
<?php
}
}
else{
echo(mysql_error());
}

Related

CodeIgniter 4 - Cannot display search result with multiple dropdown

For my website I want to display search result (on a complete new page) using the parameters in the dropdown select input. I have 4 dropdowns and I tried multiple different ways to make it work. But it doesn't seem to work!
Below is my CodeIgniter 4 code
Home Controller
The home controller file which gathers data from 4 dropdown inputs the data is fetched as an array. The array is then executed through the getSearch method located in the home model.
public function search_residential() {
$data['getfooter'] = $this->homeModel->getFooterInfo();
$params = array(
'category' => $this->request->getVar('category'),
'area' => $this->request->getVar('area'),
'bedrooms' => $this->request->getVar('bedrooms'),
'washrooms' =>$this->request->getVar('washrooms'),
);
$data['search'] = $this->homeModel->getSearch($params);
return view('search', $data);
}
Home Model
The home model contains the query of search.
//Searching data
public function getSearch($params) {
$category = $params['category'];
$area = $params['area'];
$bedrooms = $params['bedrooms'];
$washrooms = $params['washrooms'];
$builder= $this->db->table('tblresidential');
$builder->select('*');
$builder->where('1 = 1');
if ($category != '') {
$builder->where('category', $category);
}
if ($area != '') {
$builder->where('area', $area);
}
if ($bedrooms != '') {
$builder->where('bedrooms', $bedrooms);
}
if ($washrooms != '') {
$builder->where('washrooms', $washrooms);
}
$result = $builder->get();
return $result;
}
HTML Code
The HTML code that is used for execution
<div class="search__area-inner">
<?= form_open('/home/search_residential'); ?>
<div class="row">
<div class="col-6 col-lg-3 col-md-3">
<div class="form-group">
<select name="category" class="wide select_option">
<option data-display="Property Type">Property Type</option>
<option value="Property on sale">Property on Sale</option>
<option value="Property on rent">Property on Rent</option>
</select>
</div>
</div>
<div class="col-6 col-lg-3 col-md-3">
<div class="form-group">
<select name="area" class="wide select_option">
<option data-display="Select Area">Select Area</option>
<option value="2000">2000</option>
<option value="1800">1800</option>
<option value="1500">1500</option>
<option value="1200">1200</option>
<option value="900">900</option>
<option value="600">600</option>
<option value="300">300</option>
<option value="100">100</option>
</select>
</div>
</div>
<div class="col-6 col-lg-3 col-md-3">
<div class="form-group">
<select name="bedrooms" class="wide select_option">
<option data-display="Select Bedrooms">Select Bedrooms</option>
<option value="1 bedroom">1</option>
<option value="1.5 bedrooms">1.5</option>
<option value="2 bedrooms">2</option>
<option value="2.5 bedrooms">2.5</option>
<option value="3 bedrooms">3</option>
<option value="4 bedrooms">4</option>
<option value="5 bedrooms">5</option>
</select>
</div>
</div>
<div class="col-6 col-lg-3 col-md-3">
<div class="form-group">
<select name="washrooms" class="wide select_option">
<option data-display="Select Washrooms">Washrooms</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
</div>
<div class="col-12">
<div class="form-group">
<button class="btn btn-primary text-uppercase btn-block"> search
<i class="fa fa-search ml-1"></i>
</button>
</div>
</div>
</div>
<?= form_close(); ?>
</div>
Result View (the page on which result shows)
This is the page where result shows
<?php if(!empty($search)) : ?>
<section class="residential-property-list">
<div class="container">
<div class="row">
<?php foreach($search as $properties) : ?>
<div class="col-lg-12">
<div class="card__image card__box-v1">
<div class="row no-gutters">
<div class="col-md-4 col-lg-3 col-xl-4">
<div class="card__image__header h-250">
<a href="<?= base_url('/home/residential_property'); ?>/<?= $properties->id; ?>">
<img src="<?= $properties->imageone; ?>" alt="featured-image" class="img-fluid w100 img-transition">
<div class="info text-white"> for <?= $properties->category; ?></div>
</a>
</div>
</div>
<div class="col-md-4 col-lg-6 col-xl-5 my-auto">
<div class="card__image-body">
<h6 class="text-capitalize">
<?= $properties->project; ?>
</h6>
<ul class="list-inline card__content">
<li class="list-inline-item">
<span>
Bedrooms <br>
<i class="fa fa-inbox"></i> <?= $properties->bedrooms; ?>
</span>
</li>
<li class="list-inline-item">
<span>
Washrooms <br>
<i class="fa fa-map"></i> <?= $properties->washrooms; ?>
</span>
</li>
<li class="list-inline-item">
<span>
Property Facing <br>
<i class="fa fa-inbox"></i> <?= $properties->facing; ?>
</span>
</li>
<li class="list-inline-item">
<span>
Property area <br>
<i class="fa fa-map"></i> <?= $properties->area; ?>
</span>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</section>
<?php else: ?>
<br>
<br>
<div class="container"><h4 class="text-center">No Result Found Yet</h4></div>
<br>
<br>
<?php endif; ?>
The model's get(...) method returns false|ResultInterface.
To receive the actual result set,
Instead of:
(Home Model)
// ...
$result = $builder->get();
return $result;
// ...
Use this:
Result Arrays
// ...
return $builder->get()->getResult();
// ...

Arranging bootstrap cards based on id in mysql and php

I want to gather the ordered foods of each Order ID ,I mean separate them based on their order ID
here is my code :
<?php
$sql = "select * from view_orderlist_perm where state =1 ";
$result = mysqli_query($connection, $sql);
while ($row = $result->fetch_assoc()) {
?>
<div class="d-flex flex-row">
<div class="card " style="width: 210px;border: none">
<div class="card-body mr-2">
<div class="card-header">
<?php echo $row["orderId"] ?>
</div>
<ol class="list-group flex-row justify-content-between ">
<li class="list-group-item d-flex justify-content-between align-items-center"><?php echo $row['fName'] ?>
<span class="badge badge-warning ml-2 badge-pill"><?php echo $row['qty'] ?></span>
</li>
<li class="list-group-item"><input type="checkbox" class=" mr-3 mt-2 form-control"
name="<?php echo $row['orderId'] ?>"
value="<?php echo $row['fId'] ?>" id="foodReady">
</li>
</ol>
</div>
</div>
</div>
<?php
}?>
That's how I want it to looks like
And the output of my codes look like this
And here is my view

unable to display database content into specified field

i am trying to create e-commerce website. neither it is showing error not providing coding result. trying to develop popup modal it is not showing results, modal popup is appearing but the echo codes which were put to retrieve database table value, for example this command is not working. how to find mistake?
<!-- The Modal -->
<?php
require_once '../core/init.php';
<!--this code is to retreive data from database thelewala and table products-->
$id = $_post['id'];
$id = (int)$id;
$sql = "SELECT * FROM products WHERE id = '$id'";
$result = $db->query($sql);
$product = mysqli_fetch_assoc($result);
<!-- this code is to retrieve data from database thelewala and table brand-->
$brand_id = $product['brand'];
$sql = "SELECT brand FROM brand WHERE id = '$brand_id'";
$brand_query = $db->query($sql);
$brand = mysqli_fetch_assoc($brand_query);
?>
<?php ob_start(); ?>
<div class="modal fade" id="details-modal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title"><?php echo $product['title']; ?></h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-sm-6">
<div>
<img src="<?php echo $product['image']; ?>" alt="<?php echo $product['title']; ?>'" class="details img.responsive" />
</div>
</div>
<div class="col-sm-6">
<h4>Details</h4>
<p><?php echo $product['discription']; ?></p>
<hr />
<p>Price : Rs. <?php echo $product['price']; ?></p>
<p>Brand : <?php echo $brand['brand']; ?></p>
<form action="add_card.php" method="post">
<div class="form-group">
<div class="col-xs-3">
<lable for="quantity">Quantity</lable>
<input type="text" class="form-control" id="quantity" name="quantity" />
</div>
</div>
<div class="form-group">
<lable for="size">Size</lable>
<select name="size" id="size" class="form-control">
<option value=""></option>
<option value="32">32</option>
<option value="36">36</option>
<option value="XL">XL</option>
</select>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-warning" <span class="glyphicon glyphicon-shopping-cart"></spa>Add to Cart</button>
</div>
</div>
</div>
</div>
<?php echo ob_get_clean(); ?>

how to give a if else here

i want to include a else statement after the completion of the block after the second if for category equal to helathcare ....now i want another else for finance but i am not getting where excatly to place that else
i have a confusion on where to keep the else for finance and 2 more categories
now whenever i try to place the one else at the last goes unrecable to its if statement i donno its very confusing as to where to place 2 more else if statments for 2 more categories
<?php
global $row2;
if(isset($_POST['category']))
{
if($_POST['category']== 'Healthcare')
{
$query = "select *from event where category = 'Healthcare';";
$result=mysqli_query($conn,$query)or die(mysqli_error($conn));
while($row2= mysqli_fetch_array($result))
{
?>
<div class="events events-full event-list">
<div class="container">
<div class="row">
<div class="col-md-9 col-sm-8">
<!--Blog Post Start-->
<div class="blog-post">
<div class="post-thumb">
<div class="link-wrap"> <i class="fa fa-search"></i> <i class="fa fa-link"></i> </div>
<img src="images/gallery/<?php echo $row2['event_image']?>" alt='user'></div>
<div class="event-text">
<div class="event-counter"></div>
<h4> <?php echo($row2['title']); ?> </h4>
<p><?php echo($row2['descrption']); ?></p>
<p><span class="glyphicon glyphicon-map-marker"><?php echo($row2['location']); ?></span></p>
<p><span class="glyphicon glyphicon-grain"><?php echo($row2['organizer']); ?></span></p>
<a class="nd" href="">
<form action="eventdetail.php" method="post">
<input type='hidden' value="<?php echo $row2['id']; ?>" name='id'/>
<button type="submit" class="btn btn-primary" name="detail" value=”detail”>Event Detail</button>
</div>
</a> </div>
</div>
</form>
<!--Blog Post End-->
<?php }
}
}
else
{
global $row2;
$query = "select *from event;" ;
$result=mysqli_query($conn,$query)or die(mysqli_error($conn));
while($row2= mysqli_fetch_array($result))
{
?>
<div class="events events-full event-list">
<div class="container">
<div class="row">
<div class="col-md-9 col-sm-8">
<!--Blog Post Start-->
<div class="blog-post">
<div class="post-thumb">
<div class="link-wrap"> <i class="fa fa-search"></i> <i class="fa fa-link"></i> </div>
<img src="images/gallery/<?php echo $row2['event_image']?>" alt='user'></div>
<div class="event-text">
<div class="event-counter"></div>
<h4> <?php echo($row2['title']); ?> </h4>
<p><?php echo($row2['descrption']); ?></p>
<p><span class="glyphicon glyphicon-map-marker"><?php echo($row2['location']); ?></span></p>
<p><span class="glyphicon glyphicon-grain"><?php echo($row2['organizer']); ?></span></p>
<a class="nd" href="">
<form action="eventdetail.php" method="post">
<input type='hidden' value="<?php echo $row2['id']; ?>" name='id'/>
<button type="submit" class="btn btn-primary" name="detail" value=”detail”>Event Detail</button>
</div>
</a> </div>
</div>
</form>
<!--Blog Post End-->
<?php } } ?>
?>
You can't put multiple else blocks along with the if block. However, you can use multiple(as many as you want) elseif/else if blocks along with the if block, like this:
if($_POST['category'] == 'Healthcare'){
...
}elseif($_POST['category'] == 'Finance'){
...
}elseif($_POST['category'] == '...'){
...
}else{
...
}
Here's the reference: http://php.net/manual/en/control-structures.elseif.php
Part of your problem seems to be poor formatting. If you lined up your open and close tags for your if statements I think you would be able to see this better. Having said that, you can add any elseif/if clauses for the category if where I marked below:
<!--Blog Post End-->
<?php
} // closes while loop
} else{ } // closes healthcare if, add extra else here
} // closes POST if
else
{

Something wrong while deleting a row in sql table on clicking a button

Trying to delete a row from my SQL table in my shopping cart page,but my code deletes all the rows from the table on clicking a button.Even when I update quantity of the one product the quantity of all the products are changing.The answer might be the same for these two issues.Could you please review my code and tell me where I've done wrong.
Thankyou in advance
<?php
include "header.php";
require "includes/connect.php";
require "includes/product.php";
class Cartitem{
public function fetch_cart(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM cart_items WHERE user_id = ?");
$query->bindvalue(1, $_SESSION['user_id']);
$query -> execute();
return $query->fetchAll();
}
}
$cartitem= new Cartitem;
$cartitems=$cartitem-> fetch_cart();
?>
<div class="cart">
<div class = "container">
<div class="col-md-9 cart-items">
<h1 class="cart-items-h1">Cart</h1>
<hr>
<?php foreach($cartitems as $cartitem) { ?>
<div class="cart-header">
<h3>
<?php echo $cartitem['product_name'] ; ?></h3>
<br>
<?php
if(isset($_POST['delete'])){
$query = $pdo->prepare('DELETE FROM cart_items WHERE product_id=?');
$query->bindValue(1 , $cartitem['product_id']);
$query->execute();
}
?>
<form method = "POST" action="">
<button type="submit" class="close btn btn-default" name="delete"><span class="glyphicon glyphicon-remove " aria-hidden="true"></span></button>
</form>
<div class="cart-sec simpleCart_shelfItem">
<div class="cart-item cyc">
<img src="Tiger.jpg" class="img-responsive" alt=" No image"/>
</div>
<div class="cart-item-info">
<ul class="item-properties">
<li>
<form action="" method="post">
<?php
if(isset($_POST['qty'])){
$quantity=$_POST['qty'];
$query = $pdo->prepare('UPDATE cart_items SET quantity= ? WHERE product_id=?');
$query->bindValue(1 , $quantity);
$query->bindValue(2 , $cartitem['product_id']);
$query->execute();
}
?>
<select class="quantity" name="qty">
<?php
echo '<option >'.$cartitem['quantity'].'</option>';
for($q = 1 ; $q<10 ; $q++){
echo '<option >'.$q.'</option>';
}
?>
</select>
<button type="submit" class="update" name="update">Update</button>
</li>
<li><p>Rs.<?php echo $cartitem['price']; ?></p></li>
</ul>
<div class="delivery">
<p>Service Charges : Rs.190.00</p>
<span>Delivered in 2-3 bussiness days</span>
<div class="clearfix"></div>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
<?php }?>
</div>
This is (productpage.php) from where I'm storing the values to the cart table.Probably not so important to this issue. If yes
<?php
ob_start();
include ('header.php');
require('includes/connect.php');
require('includes/product.php');
$product = new Product;
if(isset ($_GET['id'])) {
$id = $_GET['id'];
$data = $product -> fetch_data($id);
if(isset($_POST['add'])){
if (isset($_SESSION['logged_in'])) {
$query = $pdo->prepare("SELECT product_id FROM cart_items WHERE product_id= ?");
$query -> bindValue(1, $id);
$query ->execute();
$num=$query->rowCount();
if($num == 0){
if(isset($_POST['qty'])){
$qty=$_POST['qty'];
}
$query = $pdo -> prepare("INSERT INTO cart_items(product_id , user_id, quantity,price,product_name) VALUES (?,?,?,?,?)");
$query -> bindValue(1, $id);
$query -> bindValue(2, $_SESSION['user_id']);
$query -> bindValue(3, $qty);
$query -> bindValue(4, $data['new_price']);
$query -> bindValue(5, $data['product_name']);
$query ->execute();
header('location:cart.php');
}
else{
echo " The product is already in your Cart";
}
}
else{
header('location:Login Page.php');
}
}
?>
<div class="showcase-grid">
<div class="container ">
<?php if(isset($error)){ ?>
<small style = "color : #aa0000"; ><?php echo $error ?></small>
<br><br>
<?php } ?>
<div class="col-md-8 showcase" id="showcase-div">
<div class="img-showcase" >
<br>
<div id="myCarousel" class="carousel slide text-center" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class=" img-responsive"></li>
<li data-target="#myCarousel" data-slide-to="1" class="img-responsive"></li>
<li data-target="#myCarousel" data-slide-to="2" class="img-responsive"></li>
<li data-target="#myCarousel" data-slide-to="3" class="img-responsive"></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<img class="slide-img" src="images/product2.jpg" alt="jwellery" >
</div>
<div class="item">
<img src="images/product3.jpg" alt="jwellery">
</div>
<div class="item">
<img src="images/product4.jpg" alt="jwellery" >
</div>
<div class="item">
<img src="images/product5.jpg" alt="jwellery" >
</div>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
<br>
</div>
</div>
<div class="col-md-4 showcase1 ">
<div class="showcase-rt-top">
<div class = "row">
<div class ="col-md-12">
<br>
<div class="pull-left Product-name">
<h3><?php echo $data['product_name']; ?></h3>
<br>
</div>
</div>
<br><br><br>
<div class ="col-xs-4 price">
<h4>Rs.<?php echo $data['new_price']; ?></h4>
</div>
<div class ="col-xs-4 oldprice text-left">
<s><h4 class="text-left">Rs.<?php echo $data['old_price']; ?></h4></s>
</div>
<div class ="col-xs-4">
<div class="pull-right rating-stars pull-left">
<h4><span class="label label-warning">3.5 <span class="glyphicon glyphicon-star star-stn" aria-hidden="true"></span></span></h5>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
<hr class="featurette-divider">
<div class="shocase-rt-bot">
<div class="col-xs-6 discount">
<?php
$discount = (($data['old_price'] - $data['new_price'])/$data['old_price'])*100;
echo round($discount) . '% off ';
?>
</div>
<form action = "" method="post" class="form-inline">
<div class="col-xs-6">
<div class="float-qty-chart">
<!-- <label class=" option">quantity:</label>-->
<select id= "select" class="form-control qnty-chrt" name="qty" >
<option value="1">quantity</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
</select>
<div class="clearfix"></div>
</div>
</div>
<ul class="ul_list">
<li class="ad-2-crt simpleCart_shelfItem">
<button type="submit" class="btn item_add" name="add" >Add To Cart</button>
<button type="submit" class="btn item_add" name="buy-now">Buy Button</button>
</li>
</ul>
<br>
</div>
</form>
<div class="features" >
<h3>product details</h3>
<ul>
<li><?php echo nl2br($data['brief_description']); ?></li>
</ul>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php
}
else{
header('location: index.php');
exit();
}
Include ('footer.php');
?>
This is the product.php
<?php
class Product {
public function fetch_all() {
global $pdo;
$query = $pdo->prepare("SELECT * FROM ear_rings_list");
$query -> execute();
return $query->fetchAll();
}
public function fetch_data($pid) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM ear_rings_list WHERE listing_id = ?");
$query->bindValue(1, $pid);
$query->execute();
return $query->fetch();
}
}
Actually you are working with an foreach loop
Just check here
<?php foreach($cartitems as $cartitem) { ?>
if(isset($_POST['delete'])){
$query = $pdo->prepare('DELETE FROM cart_items WHERE product_id=?');
$query->bindValue(1 , $cartitem['product_id']);
$query->execute();
}
if(isset($_POST['qty'])){
$quantity=$_POST['qty'];
$query = $pdo->prepare('UPDATE cart_items SET quantity= ? WHERE product_id=?');
$query->bindValue(1 , $quantity);
$query->bindValue(2 , $cartitem['product_id']);
$query->execute();
}
<?php }?>
Here you are just checking the isset($_POST['delete']) which will be true for each $cartitem['product_id'] that's why your each row is deleting and updating.
Solution:-
Use this code
<?php
include "header.php";
require "includes/connect.php";
require "includes/product.php";
class Cartitem
{
public function fetch_cart()
{
global $pdo;
$query = $pdo->prepare("SELECT * FROM cart_items WHERE user_id = ?");
$query->bindvalue(1, $_SESSION['user_id']);
$query->execute();
return $query->fetchAll();
}
}
$cartitem = new Cartitem;
$cartitems = $cartitem->fetch_cart();
?>
<div class="cart">
<div class="container">
<div class="col-md-9 cart-items">
<h1 class="cart-items-h1">Cart</h1>
<hr>
<?php
if (isset($_POST['qty']) && isset($_POST['product_id'])) {
$quantity = $_POST['qty'];
$product_id = $_POST['product_id'];
$query = $pdo->prepare('UPDATE cart_items SET quantity= ? WHERE product_id=?');
$query->bindValue(1, $quantity);
$query->bindValue(2, $product_id);
$query->execute();
}
?>
<?php
if (isset($_POST['delete']) && isset($_POST['product_id'])) {
$product_id = $_POST['product_id'];
$query = $pdo->prepare('DELETE FROM cart_items WHERE product_id=?');
$query->bindValue(1, $product_id);
$query->execute();
}
?>
<?php foreach($cartitems as $cartitem) { ?>
<div class="cart-header">
<h3>
<?php echo $cartitem['product_name']; ?></h3>
<br>
<form method="POST" action="">
<input type="hidden" value="<?php $cartitem['product_id'] ?>" name="product_id">
<button type="submit" class="close btn btn-default" name="delete"><span class="glyphicon glyphicon-remove " aria-hidden="true"></span></button>
</form>
<div class="cart-sec simpleCart_shelfItem">
<div class="cart-item cyc">
<img src="Tiger.jpg" class="img-responsive" alt=" No image"/>
</div>
<div class="cart-item-info">
<ul class="item-properties">
<li>
<form action="" method="post">
<input type="hidden" value="<?php $cartitem['product_id'] ?>" name="product_id">
<select class="quantity" name="qty">
<?php
echo '<option >' . $cartitem['quantity'] . '</option>';
for ($q = 1; $q < 10; $q++) {
echo '<option >' . $q . '</option>';
}
?>
</select>
<button type="submit" class="update" name="update">Update</button>
</form>
</li>
<li><p>Rs.<?php echo $cartitem['price']; ?></p></li>
</ul>
<div class="delivery">
<p>Service Charges : Rs.190.00</p>
<span>Delivered in 2-3 bussiness days</span>
<div class="clearfix"></div>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
<?php }?>
</div>
Changes :-
I just make an hidden input field for $cartitem['product_id'] here <input type="hidden" value="<?php $cartitem['product_id'] ?>" name="product_id">
and placed the delete and update query out side the foreach loop.

Categories