我的英语很难以理解,因为我试图在最后给出一个例子。请帮助我,我是这个领域的新手。谢谢你。
我的表格如下
<form method="post">
<p>Amount:
<input type="text" name="receive" />
Registration:
<input type="text" name="reg_number" />
<input type="submit" name="submit_amount" value="Submit" /></p>
</form>
当我提交此表单时,将运行以下查询
if(isset($_POST['submit_amount']) && count($_POST)!==0){
$receive=$_POST['receive'];
$reg_number=$_POST['reg_number'];
$sql="INSERT INTO `installments`(month, amount, receive, fk_users_id)
SELECT now(), installment, $receive, id
FROM users
WHERE reg_number = '$reg_number'";
$result=mysqli_query($con,$sql);
if($result){
echo "<script>
alert('amount inserted');
</script>";
}else{
die(mysqli_error($con));
}
}
当此查询运行时,它会从用户表格和表格中的接收列中的amount列中插入值
像:
installment_id | month | prv_arrear | amount | total | receive | arrear | fk_user_id
1 2014-09-13 2500 2500 1
我想根据此公式在prv_arrear,total和arrear列中插入值
式:
prv_arrear + amount = total
total - reveive = arrear
参见示例
installment_id | month | prv_arrear | amount | total | receive | arrear | fk_user_id
1 2014-09-12 0 2500 2500 1600 900 1
1 2014-09-13 900 2500 3400 1600 900 1
注意:当所有值都插入列中时,拖欠列的值应该在prv_arrear中,如上例所示。
答案 0 :(得分:0)
这个mysql语句应该这样做。或者非常接近。
INSERT INTO `installments`(month, amount, prv_arrear, total, arrear, receive, fk_users_id) SELECT now(), installment, installments.arrear, installment+installments.arrear, installment+installments.arrear-$receive, $receive, id FROM users LEFT JOIN installments ON users.id=installments.fk_users_id WHERE reg_number = '$reg_number' ORDER BY month DESC LIMIT 1"
答案 1 :(得分:0)
请阅读评论
<form method="post">
<p>Amount:
<input type="text" name="receive" />
Registration:
<input type="text" name="reg_number" />
<input type="submit" name="submit_amount" value="Submit" /></p>
</form>
<?php
$con= mysqli_connect('localhost', 'root', '', '25796596') or die(mysqli_error($con));
if(isset($_POST['submit_amount']) && count($_POST)!==0){
$receive=$_POST['receive'];
$reg_number=$_POST['reg_number'];
$sel="select * from installments";
$result=mysqli_query($con,$sel) or die(mysqli_error($con));
while($row= mysqli_fetch_array($result))
{
$date=$row['month'];
$installment_id=$row['installment_id'];
$amount=$row['amount'];
}
//This will update your table
$arrear=$amount-$receive;
$total=$arrear+$receive;
$update="update installments SET receive=$receive, arrear=$arrear, total=$total WHERE installment_id='$installment_id'";
$result_number= mysqli_query($con, $update) or die(mysqli_error($con));
$onemore="select * from installments";
$result= mysqli_query($con, $onemore)or die(mysqli_error($con));
while($row= mysqli_fetch_array($result))
{
$amount=$row['amount'];
$installment_id=$row['installment_id'];
$receive=$row['receive'];
$total=$row['total'];
$arrear=$row['arrear'];
}
$prv_arrear=$arrear;
$total=$amount+$prv_arrear;
//This will insert the value in the table.
$insert_query="insert into `installments`(installment_id,prv_arrear,amount,total,receive,arrear)
VALUES
('','$prv_arrear','$amount','$total','$receive','$arrear')";
$result= mysqli_query($con, $insert_query)or die(mysqli_error($con));
}