Results 1 to 4 of 4

Thread: How to prevent duplicate record insertion while refresh the php page?

  1. #1
    Join Date
    Jun 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post How to prevent duplicate record insertion while refresh the php page?

    Hi,

    I am new to php. I have form(code is below). If you click the submit button,it adds one record to the database..working fine.but after submitting,if you hitting the refresh button,it adds the same record again to the database.How to avoid that duplicate data insertion.please help me to solve this issue.And provide some code example.
    Note: after submit the form,it should display the empty form. Thanks in advance.
    Here is my code

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>

    <?php

    if($_POST['name'] == '' || $_POST['id'] == '')
    {


    $message="page refresh";
    echo $message;

    }
    else
    {
    $user = "root";
    $host="localhost";
    $pass = "admin";
    $db = "mydatabase";
    $table = "customer";
    $con=mysql_connect($host,$user,$pass) or die( "DB connection error:".mysql_error());
    $dbcon=mysql_select_db($db,$con) or die ( "Db Selection error:".mysql_error());


    $name=$_POST['name'];
    $id=$_POST['id'];
    $sql="insert into $table values ('$name','$id')";
    $result=mysql_query($sql) or die(" insert error :".mysql_error());
    }
    ?>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" name="form1">
    <table width="100%" border="1">
    <tr>
    <td>Employee Name</td>
    <td width="2%">:</td>
    <td ><input type="text" name="name" id="textfield" /></td>
    <td>Employee No</td>
    <td width="2%">:</td>
    <td><label>
    <input type="text" name="id" id="textfield2" />
    </label></td>
    </tr>

    <tr>
    <td colspan="6" align="center"><input type="submit" name="name1" value="register" />
    <input type="submit" name="id" value="cancel" /></td>
    </tr>
    </table>
    </form>



    </body>
    </html>

  2. #2
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>

    <?php

    if($_POST){
    $user = "root";
    $host="localhost";
    $pass = "admin";
    $db = "mydatabase";
    $table = "customer";
    $con=mysql_connect($host,$user,$pass) or die( "DB connection error:".mysql_error());
    $dbcon=mysql_select_db($db,$con) or die ( "Db Selection error:".mysql_error());


    $name=$_POST['name'];
    $id=$_POST['id'];
    $sql="insert into $table values ('$name','$id')";
    $result=mysql_query($sql) or die(" insert error :".mysql_error());

    if($result) echo "Successful"; else echo "Not Successful";
    }
    ?>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" name="form1">
    <table width="100%" border="1">
    <tr>
    <td>Employee Name</td>
    <td width="2%">:</td>
    <td ><input type="text" name="name" id="textfield" /></td>
    <td>Employee No</td>
    <td width="2%">:</td>
    <td><label>
    <input type="text" name="id" id="textfield2" />
    </label></td>
    </tr>

    <tr>
    <td colspan="6" align="center"><input type="submit" name="name1" value="register" />
    <input type="submit" name="id" value="cancel" /></td>
    </tr>
    </table>
    </form>



    </body>
    </html>


    This should help.. you if you have problem do post....

    Edit: Moderator Edit: No self-promotion, please.
    Last edited by djr33; 06-13-2008 at 08:00 PM.

  3. #3
    Join Date
    Jun 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hi subesh,

    Thanks for your code.It inserts the record in to db.But if you refresh the page,it adds the same record again...How to solve this?

  4. #4
    Join Date
    Feb 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,

    And a header redirect after the insertion:

    PHP Code:
    <?php

    if($_POST){
    $user "root";
    $host="localhost";
    $pass "admin";
    $db "mydatabase";
    $table "customer";
    $con=mysql_connect($host,$user,$pass) or die( "DB connection error:".mysql_error());
    $dbcon=mysql_select_db($db,$con) or die ( "Db Selection error:".mysql_error());


    $name=$_POST['name'];
    $id=$_POST['id'];
    $sql="insert into $table values ('$name','$id')";
    $result=mysql_query($sql) or die(" insert error :".mysql_error());

    if(
    $result) {

              
    header("Location: form.php");

    }else{ echo 
    "Not Successful"; }
    }
    ?>
    NOTE: php must be before the html as the header won't work if you have started output.

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    etc...
    Last edited by biggles1979; 06-13-2008 at 10:45 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •