0

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, what should i do in my code then stop automatically insert data into database, thanks in advance

<?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=  mysql_query($qry)or die(mysql_error());
        if($result){           
            echo 'SuccessFully saved data';            
        }
        else{   
            echo 'Data Not Inserted!';            
        }       
        }   
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <link rel="stylesheet" href="bootStrap/css/bootstrap.min.css">
                <link rel="stylesheet" href="bootStrap/css/bootstrap.css">
    </head>
    <body>
        <div class="container jumbotron ">

            <form action="" method="post">
             <table class="table-responsive">
            <div class="form-group form-inline">

                    <tbody> 
                    <tr>
               <td> <label for="fname" class="label-info">First Name</label></td>
                <td><input class="form-control" type="text" name="fname"></td>

                <td><label for="lname" class="label-info">Last Name</label></td>
                <td><input class="form-control" type="text" name="lname"></td>

                </tr>       
                    </tbody>

            </div>     
             </table>             
                <button type="submit" name="save_button" class="btn-success" >Save Data</button>   
   </form>            

        </div>

    </body>
</html>
5
  • 1
    you could use a unique key, then the dupes get ignored Commented Dec 16, 2016 at 2:44
  • are you sending data through ajax? Commented Dec 16, 2016 at 2:53
  • this code will not save automaticallyl on refresh. Please post the actual code. Secondly you shouldn't be using mysql_* Commented Dec 16, 2016 at 2:56
  • You are wide open to SQL injection. Also, please don't use mysql_*; the mysql_* functions are outdated, deprecated, and insecure. They were removed entirely in PHP 7. Use MySQLi or PDO instead.* Commented Dec 16, 2016 at 3:00
  • Possible duplicate of how to stop reenter data into database when page refresh Commented Dec 19, 2016 at 5:33

2 Answers 2

2

This is happening because your action is empty

Update your action to this

action="<?php echo $_SERVER['PHP_SELF']; ?>"
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

yes, this is how it should be done. But still you are promoting mysql_* :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.