【问题标题】:How to write calculation in php form without entering mysql?如何在不进入mysql的情况下以php形式编写计算?
【发布时间】: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">&nbsp;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


【解决方案1】:

我建议你的函数 return 一个值,而不是简单地回显 - 你将返回的值分配给一个你可以在其他地方使用的变量(在这种情况下,既用于显示又作为下一个函数的输入参数)

必须对您发布的代码做出某些假设,因为缺少几个变量 - 它们出现但在您共享的代码中没有定义。

此外,根据上面的评论和@Barmar 的答案中的详细说明 - 您的代码容易受到 SQL 注入的攻击,这可能会对您的网站造成巨大损害,并可能泄露非常敏感的信息。调查prepared statements 并在处理用户提供的输入时使用它们。

<?php
    function getbalance($asl,$tsl){
        return $asl - $tsl;
    }
    function gettotal($tc){
        $tc++;
        return $tc;
    }

    $errors=array();
    
    /*
        where are $annualspendinglimit, $annualbalance, $ids and $nosiri_new defined?
    */

    if( isset(
        $_POST['Submit'],
        $_POST['txtdate'],
        $_POST['txtcost'],
        $_POST['txtpanelcode']
    ) ){
    
        if( empty( $_POST['txtdate'] ) )$errors[]='Fill in date';
        if( empty( $_POST['txtcost'] ) )$errors[]='Fill in cost';
        if( $annualspendinglimit > $annualbalance )$errors[]='Annual spending limit exceed annual balance';

        
        
        if( empty( $errors ){
        
            $txtdate=$_POST['txtdate'];
            $txtcode=$_POST['txtpanelcode'];
            $txtcost=$_POST['txtcost'];
        
        
            $totalspendinglimit=gettotal( $txtcost );
            $annualbalance=getbalance( $annualspendinglimit, $totalspendinglimit );
            
            
            printf('<div>Total Spending Limit: %s, Annual Balance: %s</div>', $totalspendinglimit, $annualbalance );
            
            
            
            $sql='INSERT INTO `treatment`
                    ( `nostaf`, `nosiri`, `date`, `panelcode`, `cost` )
                  VALUES
                    (?,?,?,?,?)';
                
            $stmt=$mysqli->prepare( $sql );
            $stmt->bind_param('sssss',$ids,$nosiri_new,$txtdate,$txtcode,$txtcost);
            $stmt->execute();
            $stmt->close();
                
                
            exit( header("Location: rekod_staf2.php?nostaf=$ids") );
            
        }else{
            foreach( $errors as $error )printf('<p style="color:red;size:smaller">%s</p>',$error);
        }
    }


?>

【讨论】:

    【解决方案2】:

    您在gettotal() 函数中本地分配$totalspendinglimit,因此无法在getbalance() 中访问它。摆脱那些函数,直接在顶层代码中分配变量。

    $totalspendinglimit = $txtcost++; 递增 $txtcost,但将 $totalspendinglimit 设置为 before 递增的值。由于在那之后您再也不会使用$txtcost,因此增量是无用的。我假设您实际上想将$totalspendinglimit 设置为递增值,因此您应该只使用$txtcost + 1。您也从未分配过$txtcost,我在下面假设您的意思是$_POST['txtcost']

    使用准备好的语句而不是直接将变量替换到 SQL 中,以防止 SQL 注入。

    <?php               
    if (isset($_POST['Submit']))    
    {
        if(empty($_POST['txtdate']))
        {
            echo '<p><font color="red" size="-1">Fill in date</font></p>';
        } elseif(empty($_POST['txtcost'])){
            echo '<p><font color="red" size="-1">Fill in cost</font></p>';
        } elseif($annualspendinglimit > $annualbalance)  {
            echo '<p><font color="red" size="-1">Annual spending limit exceed annual balance</font></p>';
        } else {
            $totalspendinglimit = $_POST['txtcost'] + 1;
            echo $totalspendinglimit;
    
            $annualbalance = $annualspendinglimit - $totalspendinglimit;
            echo $annualbalance;
    
            $sqltreatment = "INSERT INTO treatment(nostaf, nosiri, date, panelcode, cost) 
                        values (?, ?, ?, ?, ?)";
            $stmt = $mysqli->prepare($sqltreatment);
            $stmt->bind_param('sssss', $ids, $nosiri_new, $_POST['txtdate'], $_POST['txtpanelcode'], $_POST['txtcost']);
            $stmt->execute();
    
            header("location: rekod_staf2.php?nostaf=$ids");
        }
    }
    ?>
    

    【讨论】:

    • 我已经应用了此代码,它成功插入数据库,但不显示总支出限额和年度余额。
    • 在分配给这些变量之前,您有 elseif ($annualspendinglimit &gt; $annualbalance)。我不确定你想在那里做什么,我认为它们是在此块之前的代码中设置的。
    猜你喜欢
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多