stop inserting data in the database when refreshing the page

后端 未结 10 1269
太阳男子
太阳男子 2020-12-06 02:27

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

<         


        
相关标签:
10条回答
  • 2020-12-06 02:59

    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';
     }
    
    0 讨论(0)
  • 2020-12-06 03:02

    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.

    0 讨论(0)
  • 2020-12-06 03:13

    The fast way is to redirect the page to it's current location:

    if (isset($_POST['submit'])) 
    {
    //do somthing
    header("Location: $current_url");
    }
    
    0 讨论(0)
  • 2020-12-06 03:18

    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');
    }
    
    0 讨论(0)
提交回复
热议问题