ArsalanK
ArsalanK

Reputation: 300

duplicate record insert on page refresh php

Whenever I entered value first time and add record its added perfectly but whenever I refresh my browser by pressing F5 it shows message "the page that you're looking for used information that you entered..." and insert duplicate record. HTML code define below:

  <?php include("Connection.php"); ?>


  <form  role="form" method="post">
                              <div class="col-md-6">
                                  <div class="form-group">
                                      <label>Type Name</label>
                                      <input name="emptype" class="form-control" placeholder="Employee ID">
                                  </div>
                                  <div  class="form-group">
                                      <label>Type Description</label>
                                      <input name="typedecs" class="form-control" placeholder="First Name">
                                  </div>





                              </div>

                              <div class="col-md-12 text-right">
                                <button type="submit" name="addtype" class="btn btn-primary">Add Type</button>
                                <button type="reset" class="btn btn-default">Reset</button>
                            </div>
</form>

below is my PHP Code


     <?php

           $emptype=$_POST['emptype'];
         $typedesc=$_POST['typedecs'];
              if(isset($_POST['addtype']))
              {

         $query=mysql_query("insert into emptype(typename,typedesc)values('$emptype','$typedesc')")or die("<script>alert('Error');</script>");


          echo '<script>showAlert();window.setTimeout(function () {HideAlert();},3000);</script>';

Does something i missing? Please Help.

Upvotes: 1

Views: 9398

Answers (3)

Wejdan
Wejdan

Reputation: 63

i think it is because the php script (insert query) is at the same page of the HTML form, so when you refresh the page you tell the browser to call the php script again with previous data. so to prevent that separate the HTML form from the php so you code should be like: 1- HTML.php

<?php include("Connection.php"); ?>
<form  role="form" method="post" action="Action.php">
                          <div class="col-md-6">
                              <div class="form-group">
                                  <label>Type Name</label>
                                  <input name="emptype" class="form-control" placeholder="Employee ID">
                              </div>
                              <div  class="form-group">
                                  <label>Type Description</label>
                                  <input name="typedecs" class="form-control" placeholder="First Name">
                              </div>
                        </div>
                          <div class="col-md-12 text-right">
                           <input type="submit" value="submit">
                        </div> </form>

2- Action.php

<?php include("Connection.php");
       $emptype=$_POST['emptype'];
     $typedesc=$_POST['typedecs'];
          if(isset($_POST['addtype']))
          {

     $query=mysql_query("insert into emptype(typename,typedesc)values('$emptype','$typedesc')")or die("<script>alert('Error');</script>");

      echo '<script>showAlert();window.setTimeout(function () {HideAlert();},3000);</script>';  echo "<meta http-equiv='refresh' content='0;url=html.php'>";

?>

this will make html.php page call Action.php page when the submit button is pressed then the php script will be executed then it will redirect you to the html.php page again

Upvotes: 0

Falt4rm
Falt4rm

Reputation: 910

One option could be to Test if the value is already in your database.

Source : example from PHP.net - mysql_query

// Formulate Query
// This is the best way to perform an SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT * FROM emptype 
    WHERE typename='%s' AND typedesc='%s'",
    mysql_real_escape_string($emptype),
    mysql_real_escape_string($typdesc));

// Perform Query
$result = mysql_query($query) or die(mysql_error());

// Get num rows
$num_rows = mysql_num_rows($result);

// If $num_rows is False - It's not a duplicate
if(!$num_rows)
...

Consider using PDO PHP.net - PDO - mysql functions are depreciated.

Upvotes: 0

Sourabh Kumar Sharma
Sourabh Kumar Sharma

Reputation: 2817

suggestion: use mysqli_* or PDO instead of mysql_* functions, but you have written code in it so i gave the code also in it.

Just run a select query to check if the record already exists in database if exists then do not insert it again.

  $query=mysql_query("SELECT * FROM emptype WHERE typename = '$emptype' AND typedesc='$typedesc') or die(mysql_error());

if (mysql_num_rows($query)<=0)
{
      $query=mysql_query("insert into emptype(typename,typedesc)values('$emptype','$typedesc')")or die("<script>alert('Error');</script>");

}

Upvotes: 4

Related Questions