我仍在尝试完成此操作,但我遇到了一个问题,我似乎无法找到将所有负值返回到0的方法。我看到负值被添加到数据库中,这会影响我的总数计算
例如,在代码底部显示日期计算。如果第一次付款是在2014-12-01到期,但后来说我的购买日期是在2014-12-01之后它将显示一个负值,我想让它在任何负值中写出0,所以它不会影响我的总计算编码。
任何帮助将不胜感激! 谢谢!
//* Using Escape Variables for Security *//
$purchase_date = mysqli_real_escape_string($con, $_POST['purchase_date']);
$mature_date = mysqli_real_escape_string($con, $_POST['mature_date']);
$amount = mysqli_real_escape_string($con, $_POST['amount']);
$unit = mysqli_real_escape_string($con, $_POST['unit']);
$purchaser_first = mysqli_real_escape_string($con, $_POST['purchaser_first']);
$purchaser_last = mysqli_real_escape_string($con, $_POST['purchaser_last']);
$purchaser_id = mysqli_real_escape_string($con, $_POST['purchaser_id']);
$purchaser_no = mysqli_real_escape_string($con, $_POST['purchaser_no']);
$purchaser_add = mysqli_real_escape_string($con, $_POST['purchaser_add']);
$beneficiary_first = mysqli_real_escape_string($con, $_POST['beneficiary_first']);
$beneficiary_last = mysqli_real_escape_string($con, $_POST['beneficiary_last']);
$beneficiary_id = mysqli_real_escape_string($con, $_POST['beneficiary_id']);
$beneficiary_no = mysqli_real_escape_string($con, $_POST['beneficiary_no']);
$beneficiary_add = mysqli_real_escape_string($con, $_POST['beneficiary_add']);
$bank_acc_name = mysqli_real_escape_string($con, $_POST['bank_acc_name']);
$bank_acc_no = mysqli_real_escape_string($con, $_POST['bank_acc_no']);
$bank_swift = mysqli_real_escape_string($con, $_POST['bank_swift']);
$bank_name = mysqli_real_escape_string($con, $_POST['bank_name']);
$bank_add = mysqli_real_escape_string($con, $_POST['bank_add']);
$bank_no = mysqli_real_escape_string($con, $_POST['bank_no']);
$agent = mysqli_real_escape_string($con, $_POST['agent']);
// Query with DATEDIFF calculation
$sql="INSERT INTO `peuicustomer` (
purchase_date, mature_date, amount, unit,
purchaser_first, purchaser_last, purchaser_id, purchaser_no, purchaser_add,
beneficiary_first, beneficiary_last, beneficiary_id, beneficiary_no, beneficiary_add,
bank_acc_name, bank_acc_no, bank_swift, bank_name, bank_add, bank_no, agent,
first_days, first_payment, second_days, second_payment, third_days, third_payment, fourth_days, fourth_payment
)
VALUES (
'$purchase_date','$mature_date','$amount','$unit','$purchaser_first','$purchaser_last',
'$purchaser_id','$purchaser_no','$purchaser_add',
'$beneficiary_first','$beneficiary_last','$beneficiary_id','$beneficiary_no','$beneficiary_add',
'$bank_acc_name','$bank_acc_no','$bank_swift','$bank_name','$bank_add','$bank_no', '$agent',
DATEDIFF('2014-12-01','$purchase_date'),
(DATEDIFF('2014-12-01','$purchase_date') * '$amount' / 365 * 0.12),
DATEDIFF('2015-03-01','2014-12-01'),
(DATEDIFF('2015-03-01','2014-12-01') * '$amount' / 365 * 0.12),
DATEDIFF('2015-06-01','2015-03-01'),
(DATEDIFF('2015-06-01','2015-03-01') * '$amount' / 365 * 0.12),
DATEDIFF('2015-09-01','2015-06-01'),
(DATEDIFF('2015-09-01','2015-06-01') * '$amount' / 365 * 0.12)
)";
// Executing and error checking of query
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "Client record has been added to the database! Redirecting URL initiated, please wait...";
// Close MySQL
mysqli_close($con);
?>
</body>
</html>
答案 0 :(得分:1)
IF('2014-12-01' > '$purchased_date',
DATEDIFF('2014-12-01', '$purchased_date'),
0
) * $amount / 365 * 0.12
与其他DATEDIFF
相同。
答案 1 :(得分:1)
考虑使用GREATEST(0, ...)
,将...
替换为任何正值或负值。该函数不会返回任何低于零的值,因为其中一个参数(如写入的)为零。
答案 2 :(得分:0)
根据@ RedFilter的回答,尝试将CASE
语句中可能为负且可能转换为0的值包装起来:
https://stackoverflow.com/a/3389734/3264286
CASE
WHEN [...potentially negative value...] < 0 then 0
ELSE [...potentially negative value...]
END