重置计算结果

时间:2018-06-11 09:20:13

标签: php mysql

我有一个程序,可以根据用户的输入从数据库中计算出一个数字。我让它工作正常,但我现在的问题是当我重置页面时,结果仍然存在。即使我在结果的变量中使用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);
}

2 个答案:

答案 0 :(得分:0)

尝试使用

$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;

也许可以帮到你

答案 1 :(得分:0)

只需检查请求是否是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;
}