尝试从我的购物车页面中的SQL表中删除一行,但是我的代码会在单击按钮时从表中删除所有行。即使我更新了一个产品的数量,所有产品的数量都在变化对于这两个问题,答案可能是一样的。请你检查我的代码并告诉我哪里做错了。
提前谢谢
<?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>
<a href="productpage.php?id=<?php echo $cartitem['product_id'] ;?> "><?php echo $cartitem['product_name'] ; ?></a></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>
这是(productpage.php),我将值存储到购物车表中。可能对此问题不那么重要。如果是的话
<?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');
?>
这是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();
}
}
答案 0 :(得分:0)
实际上,您正在使用 foreach 循环
请点击这里
DF <- structure(list(x1 = c("ontime", "Alate", "ontime", "Alate"),
x2 = c("ontime", "ontime", "Blate", "Blate")), .Names = c("x1",
"x2"), row.names = c(NA, -4L), class = "data.frame")
在这里,您只需检查为为为 {/ 1>}的 <?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 }?>
,这就是每行正在删除的原因< / strong>并更新。
<强>解决方案: - 强>
使用此代码
isset($_POST['delete'])
更改: -
我只是为$cartitem['product_id']
<?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>
<a href="productpage.php?id=<?php echo $cartitem['product_id'];?> "><?php echo $cartitem['product_name']; ?></a></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>
创建隐藏输入字段
并将删除和更新查询放在 foreach循环旁边。