【发布时间】:2016-07-09 22:34:48
【问题描述】:
我创建一个简单的 php 购物车:
- 按数量添加产品。
- 更新产品数量。
我想计算购物车中所有商品的总价,但不知道如何计算?
我用这个代码显示项目:
<?php
session_start();
$where = implode(",",$_SESSION['product']);
$get_p = $mysqli->prepare("select id,name,price,imgs,quantity from products where id IN ($where)");
$get_p->execute();
$get_p->bind_result($id,$name,$price,$imgs,$quantity);
$get_p->store_result();
while($get_p->fetch()) {
$qty = $_SESSION["qty"][$id];
$total = $qty * $price;
?>
<tr>
<td><img src="<?php echo htmlspecialchars($imgs); ?>"/></td>
<td><?php echo htmlspecialchars($name); ?></td>
<td><?php echo htmlspecialchars($price)."×".$qty; ?></td>
<td><input type="number" value="<?php echo $qty; ?>" min="1" name="qty_cart"></td>
<td><input type="submit" value="delete" name="delete<?php echo $id; ?>"/></td>
<?php
if(isset($_POST["delete".$id])) {
unset($_SESSION['product'][$id]);
}
?>
</tr>
<?php
}
?>
</table>
<div class="price_all_items">
</div>
<input type="submit" name="sub" value=""/>
</form>
【问题讨论】:
-
您错误地使用了准备好的语句。
select sum(price) where id = ? -
你的意思是我错误地使用了准备好的语句。
-
select sum(price) 这只是获取商品的价格,但我想要商品的总价格多个数量
-
@Mouradkaroudi 你没有使用准备工作。你可以只使用 $mysqli_query
-
您需要为实际值使用占位符。 php.net/manual/en/mysqli.quickstart.prepared-statements.php 然后绑定它们。将数量乘以价格。