为什么以前插入的数据插入每个页面刷新?PHP

时间:2013-12-04 05:48:56

标签: php mysql

我这里有这个代码。它成功地将数据插入到数据库中,但每次刷新浏览器时,我都会从浏览器收到此消息。要显示此页面,Firefox必须发送重复执行之前执行的任何操作(如搜索或订单确认)的信息。我按重新发送,它插入我插入的相同数据。任何人都告诉我,问题是什么,并解决它。

<?php
    mysql_connect("localhost","root","");
    mysql_select_db("task");
    if(isset($_POST['show'])) {
        mysql_query("insert into `test` (`name`) value ('".$_POST['name']."')");
        $sel=mysql_query("select * from `test` order by id DESC"); ?>

        <table>
            <?php   
            while($row=mysql_fetch_array($sel)) { ?>

                <tr>
                    <td><?php echo $row['id']; ?></td><td><?php echo $row['name']; ?></td>                  
                </tr>

            <?php } ?>
        </table>

    <?php } ?>

<html>
    <form method="POST">
        <input type="text" name="name" />
        <input type="submit" name="show" value="show" />
    </form>
</html>

4 个答案:

答案 0 :(得分:3)

您需要在代码中使用unset()函数取消设置$_POST

if(isset($_POST['show'])) {
    unset($_POST['show']); //use unset here
    mysql_query("insert into `test` (`name`) value ('".$_POST['name']."')");
    $sel=mysql_query("select * from `test` order by id DESC");?>
    <table>
        <?php while($row=mysql_fetch_array($sel)) { ?>
            <tr>
                <td><?php echo $row['id']; ?></td><td><?php echo $row['name']; ?></td>
            </tr>
        <?php } ?>
    </table>
<?php } ?>

答案 1 :(得分:1)

<?php
    if(isset($_POST['show'])) {
        mysql_query("insert into `test` (`name`) value ('".$_POST['name']."')");
        header('location:INDEX.PHP');   
    }
?>

<table>
    <?php   
        $sel=mysql_query("select * from `test` order by id DESC");
        while($row=mysql_fetch_array($sel)) { ?>
            <tr>
                <td><?php echo $row['id']; ?></td>
                <td><?php echo $row['name']; ?></td>
            </tr>
    <?php } ?>
</table>

答案 2 :(得分:0)

当您刷新已提交数据的页面时,它会重新发送请求。

请清理你的变量,你很容易受到SQL注入。

请停止使用mysql。它已被弃用,使用mysqli而不是PDO。

答案 3 :(得分:0)

使用标题功能重定向您的页面

header("location:page.php");

或使用未设置功能取消设置值

unset($variable);

有很多方法,但首先你要搜索。