我正在寻找一种方法,在刷新页面时停止在数据库中插入或发送数据。
这是我的代码:
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');
}
所以每当我刷新confirmpage.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')");
}
//best outside the if statement so user isn't stuck on a white blank page.
header("location: landing_page.php");
exit;
通过这样做,刷新的用户将刷新landing_page.php
,这意味着它不会进行两次插入。
最佳建议:先检查用户是否存在,如果存在,请不要插入!
这里的情况是,当您刷新页面时,表单会提交两次。
为了防止这种情况,您可以使用会话:
session_start();
if( $_SESSION['submit'] == $_POST['submit'] &&
isset($_SESSION['submit'])){
// user double submitted
}
else {
// user submitted once
$_SESSION['submit'] = $_POST['submit'];
}
在代码中插入或更新后,您总是需要重定向到另一个页面。
关于如何做到这一点,请参阅此处:如何在PHP中进行重定向?
(你想使用PHP重定向,而不是Javascript或HTML重定向,因为你显然真的需要它来工作。)
确认页面应该是更新后重定向到的页面,而不是插入的页面。
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;
}
防止这种情况的最佳方法是在查询后添加标头("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');
}
我通过使用会话获得了这个解决方案
<?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>';
}
?>
我们可以在不重定向的情况下停止它,使用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';
}
快速方法是将页面重定向到当前位置:
if (isset($_POST['submit']))
{
//do somthing
header("Location: $current_url");
}
我也面临同样的问题。我试图添加一些数据,每次刷新页面时都会再次添加。发生这种情况是因为页面未加载。为此,您需要添加:
<?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");
}
index.php
面临相同的问题解决方案|如果在外部查询中使用(),而在值中不使用反勾号,则使用"单引号。
<?php
include 'connection.php';
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
$query = "INSERT INTO`login`(`username`,`password`)
VALUES('$username','$password')";
$insert_data= mysqli_query( $connection,$query);
}
?>
形成
<form method="post">
<br><br>
<div class="card">
<div class="card-header bg-dark">
<h1 class="text-white text-center">Insert Opration</h1>
<h2><a href="display.php">Data</a> </h2>
</div>
<label for="username">Username</label>
<input type="text" name="username" class="form-control"><br>
<label for="password">Password</label>
<input type="password" name="password" class="form-control"><br>
<button class="btn btn-success" name="submit" type="submit">Submit</button>
</div>
</form>
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>;
}
}