为什么会出现这种情况

为什么会出现这种情况

http://img1.sycdn.imooc.com//climg/5cc0632600014e5a13600400.jpg

<?php
require '../tools.func.php';
require '../db.func.php';
require 'auth.php';
//1.从数据表中查询购物车的数据
$uid=getSession('id','shop');
$prefix=getDBPrefix();
$sql="SELECT id,price,products,quantity 
		FROM {$prefix}cart WHERE uid='$uid'";
$cart_page_data=queryOne($sql);
$cart_page_data['products']=json_decode($cart_page_data['products'],true);
//遍历查询结果
require 'header.php';
?>
<div class="cart-main-area bg__white">
	<div class="container">
		<div class="row">
			<div class="col-md-12 col-sm-12 col-xs-12">
			<?php if(hasInfo()) echo getInfo();?>
				<form action="#">
					<div class="table-content table-responsive">
						<table>
							<thead>
							<tr>
								<th class="product-thumbnail"></th>
								<th class="product-name">商品名称</th>
								<th class="product-price">单价</th>
								<th class="product-quantity">数量</th>
								<th class="product-subtotal">总计</th>
								<th class="product-remove">编辑</th>
							</tr>
							</thead>
							<tbody>
							<?php if(!empty($cart_page_data)):?>
							<?php foreach($cart_page_data['products'] as $pid=> $cart_product):?>
							<tr>
								<td class="product-thumbnail">
									<a href="product_details.php?id=<?php echo $pid;?>">
									<img src="assets/uploads/default.jpeg" alt="product img" />
									</a>
								</td>
								<td class="product-name">
								   <a href="product_details.php?id=<?php echo $pid;?>"><?php echo $cart_product['product']['name'];  ?></a>
								</td>
								<td class="product-price"><span class="amount">¥<?php echo $cart_product['product']['price'];?></span></td>
								<td class="product-quantity">
									<input type="number" disabled  value="<?php echo $cart_product['quantity']; ?>" />
								</td>
								<td class="product-subtotal">
									¥<?php echo $cart_product['product']['price']*$cart_product['quantity'];?>
								</td>
								<td class="product-remove">
									<a href="=cart_del.php?product_id=<?php echo $pid;?>">X</a>
								</td>
							</tr>
							<?php endforeach;?>
							<?php endif;?>
							</tbody>
						</table>
					</div>
					<div class="row">
						<div class="col-md-8 col-sm-7 col-xs-12">
							<div class="buttons-cart">
								<a href="index.php">继续购物</a>
							</div>

						</div>
						<div class="col-md-4 col-sm-5 col-xs-12">
							<div class="cart_totals">
								<table>
									<tbody>
									<tr class="cart-subtotal">
										<th>小计</th>
										<td><span class="amount">¥<?php echo isset($cart_page_data['price'])?$cart_page_data['price']:'0.0';?></span></td>
									</tr>
									<tr class="shipping">
										<th>快递</th>
										<td>
											<ul id="shipping_method">
												<li>
													<input type="radio" checked />
													<label>
														包邮
													</label>
												</li>
												<li></li>
											</ul>
										</td>
									</tr>
									<tr class="order-total">
										<th>总价</th>
										<td>
											<strong><span class="amount">¥<?php echo isset($cart_page_data['price'])?$cart_page_data['price']:'0.0';
											?> </span></strong>
										</td>
									</tr>
									</tbody>
								</table>

								<div class="wc-proceed-to-checkout" style="clear: both;">
									<a href="checkout.html">去付款</a>
								</div>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>

<div class="only-banner ptb--10 bg__white">
</div>
<?php 
require 'footer.php';
?>
<?php

require '../tools.func.php';
require '../db.func.php';
require 'auth.php';
$product_id=intval($_GET['product_id']);
if(empty($product_id)){
	header('location:cart.php');
	exit;
}
//查询当前购物车
$prefix=getDBPrefix();
$uid=getSession('id','shop');
$sql="SELECT id,price,products,quantity
		FROM {$prefix}cart WHERE uid='$uid'";
$cart=queryOne($sql);

// 2. 将购物车当中对应id的商品删除,更新总价格,总数量
$cart['products']=json_decode($cart['products'],true);

$cart['price']-=$cart['products'][$product_id]['quantity']*$cart['products'][$product_id]['price'];
$cart['quantity']-=$cart['products'][$product_id]['quantity'];
unset($cart['products'][$product_id]);
$cart['products']=addslashes(json_encode($cart['products']));

$sql="UPDATE {$prefix}cart 
		SET price='{$cart['price']}',
		    quantity='{$cart['quantity']}',
			products='{$cart['products']}'
			WHERE uid='$uid'";

if(execute($sql)){
	setInfo('删除成功');
}else{
	setInfo('删除失败');
}
header('location:cart.php');


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
提问者 消除bug 2019-04-25 20:20:47
  • 您好,总价对应的是从数据库中取出的price字段的值,在输出$cart_page_data信息时,总价正确吗?请查看。如果此时总价就不正确,则查看下插入此数据时操作是否正确。
    2019-04-26 09:53:02
  • 提问者 消除bug 回复 好帮手慕查理 #2
    总价就是这个总价
    2019-04-26 19:10:03
  • 提问者 消除bug 回复 好帮手慕查理 #3
    从数据库中取出的price字段的值就是这个总价
    2019-04-26 19:10:41
提问者 消除bug 2019-04-25 13:38:39

现在价格也对不上了呀

  • 您好,同学要检查下查出的数据,即$cart_page_data的结果是否正确。当能够取出购物车中数据时再进行展示。
    2019-04-25 14:21:31
  • 提问者 消除bug 回复 好帮手慕查理 #2
    能取出,但是不知道怎么更改
    2019-04-25 20:01:57
好帮手慕查理 2019-04-25 10:58:25

您好,直接将同学的代码放在源码中运行,是可以看到购物车中的内容,建议同学将获取的数据输出,查看是否获取到购物车数据。

另删除的链接多一个=。

http://img1.sycdn.imooc.com//climg/5cc121eb0001283405880082.jpg

祝学习愉快!

  • 提问者 消除bug #1
    我删除数据库中的数据,可以正常加入,但是加第二个商品又是出现这种情况,价格和商品对不上,怎么解决啊老师
    2019-04-25 20:22:42
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
PHP小白零基础入门
  • 参与学习           人
  • 提交作业       626    份
  • 解答问题       4928    个

想要学好Web后端开发的中流砥柱语言,本阶段为你轻松铺就扎实的基础,从前端网页布局的搭建到后台PHP开发,助你从零基础到掌握主流开发语言。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师