刷新页面后如何防止使用php将重复数据添加到数据库中

时间:2018-01-17 20:26:33

标签: php html mysql

我使用php和wamp服务器以及mysql数据库将数据从表单插入到数据库中,并且在将数据插入数据库之后我现在以表格形式显示数据问题是当我刷新浏览器页面时它插入重复数据进入数据库,这是我上次输入数据的副本 我的问题是如何阻止它插入重复数据这里是我的代码,我用来创建数据库和表,并在同一页面上插入数据和显示数据库中的数据。这是我的完整代码。   enter code here

            <?php 

           $servername = "localhost";
           $username = "root";
           $password = "";

          $conn = mysqli_connect($servername, $username, $password);

             if(!$conn){

            die("connection error " . mysqli_connect_error());

           }else{
            echo "connection successful <br/>";
           }

          // creating database

         $sql = "CREATE DATABASE example";

          if(mysqli_query($conn, $sql)){

           echo "database created successful <br/>";
          }else{
            echo "error creating databse <br/>" . mysqli_error($conn);
               }

          mysqli_close($conn);
               ?>

              <?php

                $servername = "localhost";
                  $username = "root";
                   $password = "";
                $databasename = "example";

           $conn = mysqli_connect($servername, $username, $password, 
             $databasename);

              mysqli_select_db($conn, "example.db");

           // creating tables

             $sql = "CREATE TABLE myexample(
            e_id int(11)  AUTO_INCREMENT PRIMARY KEY,
                company_name VARCHAR(200) ,
                  contact_no VARCHAR(200) ,
                    country_add VARCHAR(200) 
                      )";

                  if(mysqli_query($conn, $sql))
                   {
                echo "table is created successfluy:<br/>";
                      }else{

                     echo "tabe is not created <br/>" . mysqli_error($conn);
                          }
                       mysqli_close($conn);

                       ?>

                   <?php

                       $servername = "localhost";
                $username = "root";
                         $password = "";
                  $databasename = "example";

                $conn = mysqli_connect($servername, $username, $password, 
                $databasename);
                   mysqli_select_db($conn, 'example.db');
                       if ($_SERVER["REQUEST_METHOD"] == "POST")
                        {
                      if(isset($_POST['submit']))
                         {
                   $name= $_POST['compnayname'];
                        $contact = $_POST['conatct'];
                      $country = $_POST['country'];

                        $sql = "INSERT INTO myexample (
                     company_name, contact_no, country_add) 
                       VALUES ('$name', '$contact', '$country')";

                         if(mysqli_query($conn, $sql)){

                      echo "stroing of the data is successfull<br/>";
                        }else{

                      echo "data is not stored <br/>" . mysqli_error($conn);
                      }


                        }
                        }

                         mysqli_close($conn);

                          ?>

                        <!DOCTYPE html>
                          <html>
                        <head>
                       <style>
                        table {
                       font-family: arial, sans-serif;
                            border-collapse: collapse;
                      width: 100%;
                          }

                         td, th {
                          border: 1px solid #dddddd;
                           text-align: left;
                            padding: 8px;
                              }

                             tr:nth-child(even) {
                                background-color: #dddddd;
                                   }
                                      </style>
                                </head>
                                   <body>

                                   <table>
                                    <tr>
                                 <th>ID</th>
                               <th>Company</th>
                                <th>Contact</th>
                                   <th>Country</th>
                                   </tr>
                                     <tr>
                                     <?php

                   $conn = mysqli_connect($servername, $username, $password, 
                         $databasename);

                      $sql = "SELECT * FROM myexample";

                       $result = mysqli_query($conn, $sql);

                               while ($row = mysqli_fetch_array($result))
                                 {
                               $id = $row['e_id'];  
                                  $name= $row['company_name'];
                                   $contact = $row['contact_no'];
                                   $country = $row['contact_no'];

                                         ?>

                           <td><?php if(isset($id)){echo $id;} ?></td>
                             <td><?php if(isset($name)){echo $name;} ?></td>
                      <td><?php if(isset($contact)){echo $contact;} ?></td>
                      <td><?php if(isset($country)){echo $country;} ?></td>
                          </tr>
                          <?php } ?>
                              </table>
                           <br/>
                              <br/>

                       <form method="POST">
               <input type="text" name="compnayname" placeholder="compnay 
                          name">
                        <br/>
                          <br/>
                    <input type="text" name="conatct" placeholder="enter 
                       contact">
                          <br/>
                           <br/>
                  <input type="text" name="country" placeholder="enter 
                  country">
                      <br/>
                        <br/>

                 <button type="submit" name="submit">Add new Data</button>
                    </form>


                  </body>
                 </html>

1 个答案:

答案 0 :(得分:0)

每当刷新页面时,都会执行插入查询。

<?php
$servername   = "localhost";
$username     = "root";
$password     = "";
$databasename = "example";
$conn         = mysqli_connect($servername, $username, $password, $databasename);
mysqli_select_db($conn, 'example.db');
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['submit'])) {
        $name    = $_POST['compnayname'];
        $contact = $_POST['contact'];
        $country = $_POST['country'];
        $sql     = "INSERT INTO myexample (
                     company_name, contact_no, country_add) 
                       VALUES ('$name', '$contact', '$country')";
        if (mysqli_query($conn, $sql)) {
            echo "stroing of the data is successfull<br/>";
        } else {
            echo "data is not stored <br/>" . mysqli_error($conn);
        }
    }
}
mysqli_close($conn);
?>

创建新文件名insert.php并添加此代码。 并删除当前文档中的这部分代码。

然后在表单中添加动作属性

<form method="POST" action="insert.php">
  <! --your html input tag snippet here -->
</form>