php 刷新页面时停止在数据库中插入数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18115665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:59:50  来源:igfitidea点击:

stop inserting data in the database when refreshing the page

phpmysqldatabaseloadrefresh

提问by Mj Jam

I am looking for a way to stop inserting or sending data in the database when refreshing the page.

我正在寻找一种在刷新页面时停止在数据库中插入或发送数据的方法。

here is my code:

这是我的代码:

user_details_page.php

user_details_page.php

<form action="confirm_page.php" method="post" >
User Name:  
<input  type="text" name="username" >
User Email
<input  type="text" name="useremail" >
Password:  
<input  type="text" name="password" >
<input type="submit"  name="submit" >
</form>


confirm_page.php

确认页面.php

if (isset($_POST['submit'])) 
{
$user= $_POST['username'];
$email = $_POST['useremail'];
$pass= $_POST['password']; 

mysql_query("INSERT INTO table (username, useremail, email) VALUES ('$username','$useremail','$email');

}

so the problem everytime I refresh the confirm page.php the data is sent to the database. how to stop this?

所以每次我刷新确认 page.php 数据都会发送到数据库时出现问题。如何阻止这个?

回答by Sir

Header the user to a new page :

将用户标题到一个新页面:

if (isset($_POST['submit'])) 
{
  $user= $_POST['username'];
  $email = $_POST['useremail'];
  $pass= $_POST['password']; 

  mysql_query("INSERT INTO table (username, useremail, email) VALUES(`$username','$useremail','$email')");

}
//best outside the if statement so user isn't stuck on a white blank page.
header("location: landing_page.php");
exit;

By doing this the user who refreshes will be refreshing landing_page.phpwhich means it won't do the insert twice.

通过这样做,刷新的用户将刷新landing_page.php,这意味着它不会进行两次插入。

best advice: do a check to see if user exists first if so don't insert!

最佳建议:首先检查用户是否存在,如果存在,请不要插入!

回答by jh314

What is going on here is that when you refresh page, the form is submitted twice.

这里发生的事情是,当您刷新页面时,表单提交了两次。

To prevent this, you can use sessions:

为了防止这种情况,您可以使用会话:

session_start();

if( $_SESSION['submit'] == $_POST['submit'] && 
     isset($_SESSION['submit'])){
    // user double submitted 
}
else {
    // user submitted once
    $_SESSION['submit'] = $_POST['submit'];        
} 

回答by Ak Memon

We can stop it without redirect , best way to use PHP $_SESSION like this :

我们可以在不重定向的情况下停止它,这是使用 PHP $_SESSION 的最佳方式,如下所示:

if($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_SESSION['form_submit']) )
{ 
    extract($_POST);
    $sql=""INSERT INTO table (username, useremail, email) VALUES('$username','$useremail','$email')";
    $_SESSION['form_submit']='true'; 
} 
else
 {
    $_SESSION['form_submit']='NULL';
 }

回答by hazem

i have this solution by using session

我通过使用会话有这个解决方案

<?php session_start();
        if(isset($_POST[$_SESSION[a][count($_SESSION[a])-1]])){
            echo "somthing....";
            unset($_SESSION[a]);
        }
        else{     
                        echo '<form method="post">';
                              $_SESSION["a"]=array();
                              $_SESSION["a"][0]="a".rand(1,100);
                        echo '<input name="'.$_SESSION["a"][0].'"><br>';
                              $_SESSION["a"][1]="a".rand(1,100);
                        echo '<input name="'.$_SESSION["a"][1].'"><br>';
                              $_SESSION["a"][2]="a".rand(1,100);
                        echo '<input name="'.$_SESSION["a"][2].'"><br>';
                              $_SESSION["a"][3]="a".rand(1,100);
                        echo '<input type="submit" name="'.$_SESSION["a"][3].'" value="submit"><br>';
                        echo '</form>';
        }               
?>

回答by developerwjk

Once an insert or update is done in your code you always need to redirect to another page.

在代码中完成插入或更新后,您始终需要重定向到另一个页面。

See here on how to do that: How to make a redirect in PHP?

请参阅此处了解如何执行此操作:如何在 PHP 中进行重定向?

(You want to use a PHP redirect, not a Javascript or HTML one, because you obviously really need this to work.)

(您想使用 PHP 重定向,而不是 Javascript 或 HTML 重定向,因为您显然确实需要它来工作。)

The confirm page should be what you redirect to after the update, not what does the insert.

确认页面应该是您在更新后重定向到的页面,而不是插入的页面。

回答by Kim Barcelona

The best way to prevent that is to add header('Location: filename') after your query. Thus in your case,

防止这种情况的最佳方法是在查询后添加 header('Location: filename') 。因此,在您的情况下,

if (isset($_POST['submit'])) 
{
$user= $_POST['username'];
$email = $_POST['useremail'];
$pass= $_POST['password']; 

mysql_query("INSERT INTO table (username, useremail, email) VALUES ('$username','$useremail','$email');
//must be inside the condition to prevent too many redirects
header('Location: user_details_page.php');
}

回答by Sammitch

confirm_page.php:

确认_page.php:

if (isset($_POST['submit'])) 
{
$user= $_POST['username'];
$email = $_POST['useremail'];
$pass= $_POST['password']; 

mysql_query("INSERT INTO table (username, useremail, email) VALUES ('$username','$useremail','$email')"); // <-- missing endquote and bracket here

header('Location: somewhere_else.php');
exit;
}

回答by Eyal Sooliman

The fast way is to redirect the page to it's current location:

快速的方法是将页面重定向到它的当前位置:

if (isset($_POST['submit'])) 
{
//do somthing
header("Location: $current_url");
}

回答by jsLearner

I was facing the same problem. Was trying to add some data and everytime when I refreshed the page it gets added again. It happens because the page is not loading. For that you need to add:

我面临着同样的问题。试图添加一些数据,每次刷新页面时,它都会再次添加。发生这种情况是因为页面未加载。为此,您需要添加:

<?php ob_start();?>in your header file, then include the header in the current file you are working on. After that use the below code

<?php ob_start();?>在您的头文件中,然后在您正在处理的当前文件中包含该头文件。之后使用下面的代码

    if (isset($_POST['submit'])) 
{
  $user= $_POST['username'];
  $email = $_POST['useremail'];
  $pass= $_POST['password']; 

  mysql_query("INSERT INTO table (username, useremail, email) VALUES(`$username','$useremail','$email')");

//user it before if statement
header("location: onthesamepage/oranyotherpage.php");
}

回答by Vishnu S

if($_POST(submit)
{
 //database insertion query
 // if successful insertion
    {
echo"<script language='javascript'>alert('successfully inserted')</script>";
echo"<script>document.location='your_page.php';</script>;
    }
}