how to stop data automatically insert into database in php

后端 未结 2 1889
一生所求
一生所求 2021-01-27 05:14

I have problem in this code. In this code when i press save data button , the data insert into database but when i refresh page then it\'s automatically insert into database, wh

相关标签:
2条回答
  • 2021-01-27 06:02

    This is happening because your action is empty

    Update your action to this

    action="<?php echo $_SERVER['PHP_SELF']; ?>"
    
    0 讨论(0)
  • 2021-01-27 06:02

    Make a separate php file that will insert data to database. Give this in the form action attribute.

    <form action="insert.php" method="post">
         ......
         ......
    </form>
    

    insert.php file

    <?php 
    require './database/databaseConnection.php';
    
    if(isset($_POST['save_button']))
        {   
        if(($_POST['fname']&&($_POST['lname'])))
            {
            $first_name=$_POST['fname'];
            $last_name=$_POST['lname'];
            $qry="INSERT INTO user_master(first_name,last_name) values('$first_name','$last_name')";
            $result=  mysqli_query($qry)or die(mysql_error());
            if($result){           
                echo 'SuccessFully saved data';            
            }
            else{   
                echo 'Data Not Inserted!';            
            }       
            }   
        }
    ?>
    

    You can use header() to redirect to your previous page if you want. Thus not allowing the refreshing of insert.php

    header("location: your_page.php");
    

    it will be safe if you use Prepared Statements Take a look

    0 讨论(0)
提交回复
热议问题