<?php
$name = $_POST['name'];
$brand = $_POST['brand'];
$size = $_POST['size'];
$quantity = $_POST['quantity'];
$totalamount = 0.00;
$con=mysqli_connect('localhost', 'username', 'password', 'database');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT name,quantity,size,price FROM database.store
WHERE name='$name' and manufacturer='$brand' and quantity='$quantity' and size='$size';");
$row = mysqli_fetch_array ($result);
$totalamount=$quantity * $row['price'];
echo "<br />Your subtotal is: $".number_format($totalamount,2)."<br />";
mysqli_query($con,"UPDATE store SET quantity=quantity-'$quantity' WHERE name='$name' AND size='$size'");
mysqli_close($con);
?>
此php的目标是允许用户购买商品。当用户购买东西时,数据库会相应更新。
我现在的问题是,输出没有显示正确的小计,它只显示:“你的小计是:$ 0.00”。但我的数据库正确更新了用户购买的商品数量。