我正在寻找一种在刷新页面时停止在数据库中插入或发送数据的方法。
这是我的代码:
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');
}
所以问题每次我刷新确认page.php时数据都会被发送到数据库。怎么阻止这个?
答案 0 :(得分:16)
将用户标题为新页面:
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
,这意味着它不会执行两次插入。
最佳建议:如果不插入,请先检查用户是否存在!
答案 1 :(得分:2)
这里发生的是当你刷新页面时,表格会被提交两次。
为防止这种情况,您可以使用会话:
session_start();
if( $_SESSION['submit'] == $_POST['submit'] &&
isset($_SESSION['submit'])){
// user double submitted
}
else {
// user submitted once
$_SESSION['submit'] = $_POST['submit'];
}
答案 2 :(得分:0)
在代码中完成插入或更新后,您始终需要重定向到另一个页面。
请参阅此处了解如何执行此操作:How to make a redirect in PHP?
(您希望使用PHP重定向,而不是Javascript或HTML重定向,因为您显然确实需要这样做。)
确认页面应该是您在更新后重定向到的内容,而不是插入内容。
答案 3 :(得分:0)
防止这种情况的最佳方法是在查询后添加标题('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');
}
答案 4 :(得分:0)
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 :(得分:0)
我使用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>';
}
?>
答案 6 :(得分:0)
我们可以在没有重定向的情况下停止它,最好的方式就是使用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';
}
答案 7 :(得分:0)
快速方法是将页面重定向到当前位置:
if (isset($_POST['submit']))
{
//do somthing
header("Location: $current_url");
}
答案 8 :(得分:0)
我面临着同样的问题。试图添加一些数据,每次刷新页面时,它都会再次添加。发生这种情况是因为页面未加载。为此,您需要添加:
<?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");
}
答案 9 :(得分:0)
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>
答案 10 :(得分:-1)
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>;
}
}