stop inserting data in the database when refreshin

2019-01-18 13:32发布

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

<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

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?

9条回答
放荡不羁爱自由
2楼-- · 2019-01-18 13:54

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>';
        }               
?>
查看更多
可以哭但决不认输i
3楼-- · 2019-01-18 13:55
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>;
    }
}
查看更多
爷的心禁止访问
4楼-- · 2019-01-18 13:57

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

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';
 }
查看更多
不美不萌又怎样
5楼-- · 2019-01-18 14:02

confirm_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;
}
查看更多
乱世女痞
6楼-- · 2019-01-18 14:06

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?

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

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

查看更多
祖国的老花朵
7楼-- · 2019-01-18 14:07

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

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');
}
查看更多
登录 后发表回答