重置计算结果

efzxgjgh  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(274)

我有一个程序,用用户的输入从数据库中计算出一个数字。我使它工作良好,但我现在的问题是,当我重置页面,结果仍然存在。即使我使用 unset 在结果的变量中,它仍然保持不变。这是我的密码:

<?php
  include 'dbconnect.php'
?>
<html>
<head>
</head>
<body>
  <br>
  <br>
  <br>
  <div align="center">
    <form method="POST">
        <?php
            unset($res);
            $fetch = "SELECT Valor FROM taxas WHERE Id = 5";
            $send = mysqli_query($con, $fetch);
            while ($row = mysqli_fetch_array($send)) {
                $value = $row['Valor'];
            }
            echo $value;
            if (isset($_POST['op'])) {
                $num1 = $_POST['n1'];
            }
            $res = $value + $num1;
        ?>
        *<input type="text" name="n1"> 
        <button name="op"> = </button>
        <?php
            echo $res;
        ?>
    </form>
</div>
</body>
</html>

如果需要,“dbconnect.php”的代码:

<?php
$place = "localhost";
$user = "root";
$pass = "";
$database = "teste2";
$con = mysqli_connect ($place, $user, $pass, $database);
if ($con->connect_error) {
    die("Error: " . $con->connect_error);
}
ubof19bj

ubof19bj1#

只需检查请求是否为post请求,并仅在以下情况下进行计算:

$res = '';
if (!empty($_POST)) {
   // your calculations
   $fetch = "SELECT Valor FROM taxas WHERE Id = 5";
   $send = mysqli_query($con, $fetch);
   while ($row = mysqli_fetch_array($send)) {
       $value = $row['Valor'];
   }
   echo $value;
   if (isset($_POST['op'])) {
       $num1 = $_POST['n1'];
   }
   $res = $value + $num1;
}
voase2hg

voase2hg2#

尝试使用

$res = '';
$fetch = "SELECT Valor FROM taxas WHERE Id = 5";
$send = mysqli_query($con, $fetch);
while ($row = mysqli_fetch_array($send)) {
    $value = $row['Valor'];
}
echo $value;
if(isset($_POST['op'])) {
    $num1 = $_POST['n1'];
}
$res = $value + $num1;

也许对你有帮助

相关问题