【问题标题】:PHP Reload (F5) of success page will call database update again成功页面的 PHP Reload (F5) 将再次调用数据库更新
【发布时间】:2016-02-28 02:24:52
【问题描述】:

我有 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/重新加载再次调用该操作时禁用该操作?

【问题讨论】:

  • 与其他人一样:将浏览器移至不同的页面。
  • 最简单的方法是成功后将浏览器重定向到不同的页面;将echo("Success"); 移动到新页面/脚本(我们将其命名为success.php)并在send_exec.php 中将其替换为header('Location: /success.php');
  • 因为没有人提到这一点,我会这样做。请不要使用 mysql_ 函数,因为它们已被弃用并且在 PHP 7.0 中已被删除。请使用mysqli_函数或使用PDO
  • 谢谢,我不知道这个!

标签: php mysql database forms reload


【解决方案1】:

制作一个名为“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);
?> 

【讨论】:

  • 不要将重定向放入send() 函数中。让它返回一个布尔值(数据库操作的结果)并让调用代码决定下一步做什么(重定向到不同的页面)。这样,该函数可以在不同的上下文中再次使用,其中延续可以不同。
  • 非常感谢!这对我帮助很大!
  • @emil-elkjær-nielsen 有没有办法在这里也返回一个值:else { header("Location: done.php?msg=success"); }; Like return($new_points);但是我如何获得页面 done.php 的值?
  • 我可能会创建一个有点像这样的函数: function getPoints($account){ //do stuff here return $YourVarNameHere;在第三页上,然后从 done.php 调用该函数
  • 谢谢,我试试看。
【解决方案2】:

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

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

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

【讨论】:

  • 这不是页面刷新的工作方式。当用户要求浏览器刷新页面时,之前的请求按原样重新发送(使用与之前相同的方法和参数)。
  • 那么在这种情况下,我们可以使用 header('location:filename.php') ,它将带回原始页面的表单。对吗?
  • 这种情况通常通过在表单提交成功处理后将浏览器(确实使用header('Location: ...');)重定向到另一个页面来处理。这个新页面显示提交处理的结果。它可以从 GET 参数或 cookie(会话)了解处理是如何进行的。当提交的数据未验证以及应用程序逻辑允许使用不同的数据再次提交表单时,显示表单页面是有意义的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
相关资源
最近更新 更多