问题
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?
回答1:
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.php which means it won't do the insert twice.
best advice: do a check to see if user exists first if so don't insert!
回答2:
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'];
}
回答3:
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.
回答4:
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;
}
回答5:
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');
}
回答6:
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>';
}
?>
回答7:
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';
}
回答8:
The fast way is to redirect the page to it's current location:
if (isset($_POST['submit']))
{
//do somthing
header("Location: $current_url");
}
回答9:
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
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");
}
回答10:
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>;
}
}
来源:https://stackoverflow.com/questions/18115665/stop-inserting-data-in-the-database-when-refreshing-the-page