【发布时间】:2021-04-10 23:36:56
【问题描述】:
我在使用函数时遇到了计算问题。我希望直接计算总支出限额和年度余额而不输入 SQL,并且从 SQL 中检索唯一的年度支出限额。年度支出限额是插入治疗表的txtcost的附加数据。成功插入数据库,但不显示totalspendinglimit和年度余额。
<?php
if (isset($_POST['Submit']))
{
if(empty($_POST['txtdate']))
{
echo '<p><font color="red" size="-1">Fill in date</font></p>';
}
else
if(empty($_POST['txtcost'])){
echo '<p><font color="red" size="-1">Fill in cost</font></p>';
}
else {
if($annualspendinglimit > $annualbalance) {
echo '<p><font color="red" size="-1">Annual spending limit exceed annual balance</font></p>';
}
else {
function gettotal($txtcost){
$totalspendinglimit = $txtcost++;
echo $totalspendinglimit;
}
gettotal($txtcost);
function getbalance($annualspendinglimit,$totalspendinglimit){
$annualbalance = $annualspendinglimit - $totalspendinglimit;
echo $annualbalance;}
getbalance($annualspendinglimit,$totalspendinglimit);
$sqltreatment = "INSERT INTO treatment(nostaf, nosiri, date, panelcode, cost)
values ('$ids', '$nosiri_new', '$_POST[txtdate]','$_POST[txtpanelcode]', '$_POST[txtcost]')";
mysqli_query($mysqli,$sqltreatment);
header("location: rekod_staf2.php?nostaf=$ids");
}
}
}
?>
totalspendinglimit、annualspendinglimit和annualbalance的前端代码
<td width="141"><font face="Tahoma">Total spend</font></td>
<td width="173"><font face="Tahoma"><strong>RM <font face="Tahoma">
<? $totalspendinglimit; ?>
</font></strong></font></td>
</tr>
<tr bgcolor="#0099FF">
<td><font face="Tahoma"> Limit Spend</font></td>
<td class="style71"><strong><font face="Tahoma">RM
<?=$r['annualspendinglimit']?>
</font></strong></td>
<td class="style71"><font face="Tahoma">Total balance</font></td>
<td class="style71"><font color="#FF0000" face="Tahoma"><strong>RM
<? $annualbalance;
?>
</strong></font></td>
【问题讨论】:
-
不要把函数定义放在
if/else里面,把它们放在顶层。 -
为什么还需要定义这些函数?只需将计算直接放在
else块中即可。 -
由于在 sql 命令中直接使用用户提供的数据,您的代码容易受到 sql 注入的影响。请改用Prepared Statements
-
您分配的变量是函数的本地变量,您无法在其他函数中访问它们。
-
你在哪里设置
$txtcost?
标签: php html mysql mysqli php-7