PHP成功页面的重新加载(F5)会再次调用数据库更新

PHP Reload (F5) of success page will call database update again

我有 3 个 php 页面,在第一页我有一个表格,用户可以在其中输入他的帐户(电子邮件地址)和他想从他的帐户中扣除的金额。 在第二个上,我调用了函数并显示了成功消息。 在第三页上,函数 send() 完成,它打开数据库连接,更改帐户金额并保存。

第一页:

<form action="send_exec.php" method="post">
Amount: <input type="number" size="24" maxlength="4"
name="points_send" min="0"><br><br>
Account<input type="text" size="24" maxlength="50"
name="email"><br><br>
<input type="submit" value="Send">
</form>

第二页(send_exec.php):

<?php
include("update_send.php");
$email = $_POST["email"];
$points_send = $_POST["points_send"];
settype ($points_send, "integer"); 
send($email, $points_send);
echo ("Success");
?> 

第三页(update_send.php):

//some calculations are done before this to get $new_points
function send($email, $points_send)
{
$mysql_update_points = "UPDATE user SET points = ('$new_ponints') WHERE email = ('$email')";
$mysql_update = mysql_query($mysql_update_points);
}

现在的问题是,当我在第一页上执行操作时,我在第二页上结束了:如何在浏览器中单击 F5/重新加载时再次调用该操作时禁用它?

我认为你需要检查表单是否已提交。

您可以使用以下代码进行检查: 如果 ($_SERVER["REQUEST_METHOD"] == "POST") { ... }

请在继续之前检查 form validations...

制作第四页,名为 "done.php"。

代码:

<?php
$pageMsg = isset($_GET['msg']) ? $_GET['msg'] : '';

if($pageMsg === "success"){
      echo "Success";
   } else {
      echo "Error";
   };
?>

然后在第三页将您的来源更改为:

//some calculations are done before this to get $new_points
function send($email, $points_send)
{
$mysql_update_points = "UPDATE user SET points = ('$new_ponints') WHERE email = ('$email')";
$mysql_update = mysql_query($mysql_update_points);

if($mysql_update === FALSE) { 
    //die(mysql_error()); //Remove comment if you want to get the error.
    header("Location: done.php?msg=error");
} else {
    header("Location: done.php?msg=success");
    };
}

最近将您的第二页(send_exec.php)更改为:

<?php
include("update_send.php");
$email = $_POST["email"];
$points_send = $_POST["points_send"];
settype ($points_send, "integer"); 
send($email, $points_send);
echo ("Success");
?> 

为此:

<?php
require_once("update_send.php");
$email = $_POST["email"];
$points_send = $_POST["points_send"];
settype ($points_send, "integer"); 
send($email, $points_send);
?>