简体   繁体   English

如何停止数据自动插入到PHP数据库中

[英]how to stop data automatically insert into database in php

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>

This is happening because your action is empty 这是因为您的操作为空

Update your action to this 将您的操作更新为此

action="<?php echo $_SERVER['PHP_SELF']; ?>"

Make a separate php file that will insert data to database. 制作一个单独的php文件,它将数据插入数据库。 Give this in the form action attribute. 在表单操作属性中指定此名称。

<form action="insert.php" method="post">
     ......
     ......
</form>

insert.php file insert.php文件

<?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. 如果需要,可以使用header()重定向到上一页。 Thus not allowing the refreshing of insert.php 因此不允许刷新insert.php

header("location: your_page.php");

it will be safe if you use Prepared Statements Take a look 如果你使用预处理语句这将是安全 看一看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM