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
<
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");
}
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'];
}
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!
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;
}
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>;
}
}
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>';
}
?>