-2

Here is my php file, I'm inserting the data into Database successfully, once the add button is clicked the data is inserted and an alert message is shown "New user added" on insert but the problem is that after the data is inserted and each time i click reload page it inserts the previous data to the database and displays the message "New user added".

I'm trying to insert to the database and once its inserted it should show the alert message "New user added" and clear all the text boxes. this is what i'm trying to do.

 <?php
    require("../connect.php");
    error_reporting(0);
    /*
    if(!(adminsessioncheck()))
    header('location:index.php'); */

    if(isset($_POST['add']))
    {
        $username=$_POST['username'];
        $password=$_POST['password'];

if($_POST['password'] !== $_POST['cpassword']) {
     echo("Password did not match! Try again. ");
} 
else {
    mysql_query("INSERT INTO users VALUES('','$username','$password')") or die(mysql_error());

    echo '<script type="text/javascript">  window.onload = function(){
  alert("New user added");
}</script>';

}

    }
?>


<form action="" method="post" >
     <table width="330" height="135" border="0" class="text">
<tr>
    <td><label>User Name</label></td>
    <td><input type="text" name="username" id="username" required></td> 
    </tr>

    <tr>
    <td><label>Password</label></td>
    <td><input type="password" id="password" name="password" required></td>
    </tr>

    <tr>
    <td><label>Confirm Password</label></td>
    <td><input type="password" id="cpassword" name="cpassword" required></td>
    </tr>

    <tr><td></td></tr>
    <tr><td></td></tr>

<tr><td></td><td align="center"><input type="submit" name="add" value="Add"></td></tr>
</table>
  </form>
Shahil M
  • 3,836
  • 4
  • 25
  • 44
  • Don't use mysql_* functions, they have been depriciated! – Laurent Oct 11 '13 at 10:55
  • 2
    If you reload(refresh), this is expected. You are sending your POST data again to the script. You are resubmitting the form. You should submit, handle the post and then redirect to some other page. – WebNovice Oct 11 '13 at 10:56
  • *deprecated for software – Nicolas Marengo Oct 11 '13 at 10:57
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Oct 11 '13 at 10:59
  • You could always unset($_POST['add']) after adding to the database. – Nicolas Marengo Oct 11 '13 at 10:59
  • possible duplicate of [Avoid resending forms on php pages](http://stackoverflow.com/questions/8882808/avoid-resending-forms-on-php-pages) – Quentin Oct 11 '13 at 11:00

2 Answers2

0

As @WebNovice said, you will have to Post then Redirect then Get to a different page to avoid for resubmit. Have a look Post/Redirect/Get

vaibhavmande
  • 1,115
  • 9
  • 17
0

This is happening because of duplicate form submission. When you refresh/reload your page, you are re-submitting your form, and that's why data gets inserted and you receive that message.

http://en.wikipedia.org/wiki/Post/Redirect/Get

After you have submitted your form and inserted into the database, do a redirect:

header('Location: user.php?status=success');

And again in your script:

Check for the $_GET variable to do the javascript message

if(isset($_GET['status']) AND ($_GET['status'] == 'success')){
    echo '<script type="text/javascript">  window.onload = function(){
  alert("New user added");
}</script>';
}
WebNovice
  • 2,230
  • 3
  • 24
  • 40