我正在做一个大学项目。我一直试图实现一些逻辑计算,但似乎无法得到任何想法。这是我正在处理的表格式的简化版本:
id quantity price total received balance
John 20 10 200 0 200
John 0 0 0 200 0
John 0 0 0 400 -400
Ash 0 0 0 550 -550
Ash 35 20 700 0 150
我想:
计算并更新总数=总数=数量*价格。为此我尝试在SQL中运行查询,如UPDATE mytable SET total = quantity * price
,但它只在我在SQL中运行它时更新'total'列,但我想动态更新它。我还尝试了ALTER TABLE mytable DROP COLUMN total
和ALTER TABLE Sales ADD total AS quantity*price
,但它提供了语法错误。
想要为每个id计算余额
balance(current)= balance(last balance) + total cost
(对于当前商品)和balance(current) = balance(last balance) - amount
(当金额列中有'金额'值时)
从我用作此MySQL表的输入的其他表生成的id。这里是我想要实现这两个逻辑的代码,用于输入user_id的代码,单价和数量:
<?php
//The line of code below runs the connect script, this allows you to connect to your sql datebase throughout the rest of the code.
include_once"scripts/connect.php";
//The next lines gather the information entered into form and turns them into php variables so they can be entered into the database
$user_id = $_POST['user_id'];
$category = $_POST['category'];
$particulars = $_POST['particulars'];
$quantity = $_POST['quantity'];
$unit_price = $_POST['unit_price'];
if ($user_id!='' || $username!=''){
$dat=$_POST['day']."-".$_POST['month']."-".$_POST['year'];
//This bit places the php variables into the correct columns in your database
$sqlcreate = mysql_query("INSERT INTO products_payments_user(user_id, category, date, particulars, quantity, unit_price)
VALUES('$user_id','$category','$dat','$particulars','$quantity','$unit_price')");
//produces a message to let you know if the data has been successfully submited or not.
if ($sqlcreate){
$msg = '<font color="#009900">Successfully entered.</font>';
} else {
$msg = '<font color="#FF0000">Problems connecting to server, please try again later.</font>';
}
echo $msg;
}
else
{
echo "<h2>"."**Please Fill All Fields**"."</h2>";
}
?>
对于付费金额信息,我使用了另一个PHP表单。因此,除了“总计”和“平衡”列之外,所有数据都在我们的简化表中显示,需要逻辑实现为1和2.