我有一个包含
列的账单表id dish_bill electric_bill telephone_bill mobile_bill total
我想在每列中插入数据。当我提交数据时,所有列的总和将插入每行的总字段中。
<?php
$dbHost = "localhost";
$dbUser = "root";
$dbPassword = "";
$dbName = "bill";
if(isset($_POST['sub'])){
$dishBill = $_POST['dish'];
$electricBill = $_POST['electric'];
$telephoneBill = $_POST['telephone'];
$mobileBill = $_POST['mobile'];
$query = "insert into bills(dish_bill,electric_bill,telephone_bill,mobile_bill,total) values ($dishBill,$electricBill,$telephoneBill,$mobileBill,sum($dishBill+$electricBill+$telephoneBill+$mobileBill))";
$conn = mysqli_connect($dbHost,$dbUser,$dbPassword,$dbName);
mysqli_query($conn,$query);
}
?>
<form action="#" method="post">
Dish Bill: <input type="text" name="dish">
Electric Bill: <input type="text" name="electric">
Telephone Bill: <input type="text" name="telephone">
Mobile Bill: <input type="text" name="mobile">
<input type="submit" name="sub">
</form>
答案 0 :(得分:0)
查询中的sum()是什么? SQL SUM()函数用于SELECT语句。
您可以在查询之前计算总和,例如
$sum = $dishBill + $electricBill + $telephoneBill + $mobileBill;
然后在查询中使用$sum
而不是sum($dishBill+$electricBill+$telephoneBill+$mobileBill)
同样在计算$sum
时,请确保将您的值转换为整数或浮点数或任何您需要的值(例如浮点floatval($dishBill)
等)