stop inserting data in the database when refreshing the page [duplicate] stop inserting data in the database when refreshing the page [duplicate] database database

stop inserting data in the database when refreshing the page [duplicate]


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!


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'];        } 


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.