每当我重新加载/刷新我的页面时,它会使mysql查询加倍? [PHP]

时间:2014-08-23 13:20:00

标签: php mysql forms isset repeat

我正在使用我们的项目,我注意到每当我刷新页面时,mysql查询都会重复。单击提交按钮时,它将转到同一页面,它将执行查询。即使我在提交按钮中使用了isset()方法,但在刷新/重新加载页面时仍然会重复查询。谢谢 :) !

<html>
<body>

<head>
<link rel="stylesheet" type="text/css" href="Homepagestyle.css">
</head>
<form method = "POST" action = "Forum.php">

<?php
session_start();

    mysql_connect("127.0.0.1", "root", "toor");
    mysql_select_db("matutorials");



    echo "Welcome " . "<a href = 'UserProf.php'>". $_SESSION['username'] . "</a> <br>";


    if (isset($_POST['btnProg'])){
        echo $_SESSION['prog'] . "<br>";
    } else if (isset($_POST['btnNet'])){
        echo $_SESSION['net'] . "<br>";
    }
?>

<center><font face = 'verdana'><textarea cols = 70 rows = 6 name = 'txtpost'></textarea></font></center><br>
<center><input type = 'submit' name = 'btnPost'></center><br> <br>
<center><table>


<?php


            if (isset($_POST['btnProg'])){
                $_SESSION['pasamoto'] = 1;

                $capRows = "SELECT * FROM page_post WHERE category_id = 1 ORDER BY timestamps DESC";
                $iQuer = mysql_query($capRows);

                while ($getRows = mysql_fetch_array($iQuer)){
                echo "<tr>";
                echo "<td><div id = 'postsdiv'>" . $getRows['post'] . "</div><br>";
                echo "</tr>";
                }
            }



?> 


</table> </center>

<?php

session_start();
    if(isset($_POST['btnPost'])){
    $post_content = $_POST['txtpost'];
    $dttime = date("Y-m-d") . " " . date("h:i:sa");
    $var = $_SESSION['pasamoto'];


        if ($var == 1){
            $addpost = "INSERT INTO page_post(post,timestamps,category_id) VALUES ('$post_content','$dttime','$var')";
            mysql_query($addpost);
            $capRows = "SELECT * FROM page_post WHERE category_id = '".$var."' ORDER BY timestamps DESC";
            $iQuer = mysql_query($capRows);


                while ($getRows = mysql_fetch_array($iQuer)){
                echo "<tr>";
                echo "<td><div id = 'postsdiv'>" . $getRows['post'] . "</div><br>";
                echo "</tr>";
                }
        }       
        //}

        if ($var == 2){
            $addpost = "INSERT INTO page_post(post,timestamps,category_id) VALUES ('$post_content','$dttime','$var')";
            mysql_query($addpost);
            $capRows = "SELECT * FROM page_post WHERE category_id = '".$var."' ORDER BY timestamps DESC";
            $iQuer = mysql_query($capRows);


                while ($getRows = mysql_fetch_array($iQuer)){
                echo "<tr>";
                echo "<td><div id = 'postsdiv'>" . $getRows['post'] . "</div><br>";
                echo "</tr>";
                }

        }







    //}


?>
</form>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

如果您在提交表单后刷新页面,POST仍会被某些浏览器识别,并将导致代码的第二次POST。您应该更新代码以在单独的页面或函数上触发SQL查询,然后将用户重定向到成功/感谢页面,其中刷新不会复制查询。

或者,您可以在页面上有一个包含唯一令牌的隐藏字段,并将其与Cookie进行比较。在页面加载时,您将令牌保存到cookie和表单上的隐藏字段。提交表单时,验证隐藏表单字段中的令牌是否与cookie匹配,然后删除cookie。提交后刷新页面将导致令牌验证失败,从而防止重复的SQL插入。

答案 1 :(得分:0)

在插入查询后,通过重定向页面来清除表单数据 喜欢标题('location:home.php')

答案 2 :(得分:0)

正如@PeeHaa在上述评论中所建议的那样,使用 Post-Redirect-Get 概念。
修改了你的代码。请尝试以下:

<强> Forum.php     

<head>
<link rel="stylesheet" type="text/css" href="Homepagestyle.css">
</head>

<body>

<form method="POST" action="Forum.php">

<?php
    session_start();

    mysql_connect("127.0.0.1", "root", "toor");
    mysql_select_db("matutorials");

    echo "Welcome " . "<a href = 'UserProf.php'>". $_SESSION['username'] . "</a> <br>";

    if (isset($_GET['show']))
    {
        echo $_SESSION['prog'] . "<br>";
    } 
    else if (isset($_GET['show']))
    {
        echo $_SESSION['net'] . "<br>";
    }
?>

<center><font face = 'verdana'><textarea cols = 70 rows = 6 name = 'txtpost'></textarea></font></center><br>
<center><input type = 'submit' name = 'btnPost'></center><br> <br>
<center><table>

<?php
    if (isset($_GET['show'])) 
    {
        $_SESSION['pasamoto'] = 1;

        $capRows = "SELECT * FROM page_post WHERE category_id = 1 ORDER BY timestamps DESC";
        $iQuer = mysql_query($capRows);

        while ($getRows = mysql_fetch_array($iQuer))
        {
            echo "<tr>";
            echo "<td><div id = 'postsdiv'>" . $getRows['post'] . "</div><br>";
            echo "</tr>"; 
        }
     }
?> 

</table> </center>

<?php
    if(isset($_POST['btnPost']))
    {
        $post_content = $_POST['txtpost'];
        $dttime = date("Y-m-d") . " " . date("h:i:sa");
        $var = $_SESSION['pasamoto'];

        if ($var == 1)
        {
           $addpost = "INSERT INTO page_post(post,timestamps,category_id) VALUES ('$post_content','$dttime','$var')";
            mysql_query($addpost);

            $capRows = "SELECT * FROM page_post WHERE category_id = '".$var."' ORDER BY timestamps DESC";
            $iQuer = mysql_query($capRows);


            while ($getRows = mysql_fetch_array($iQuer))
            {
                echo "<tr>";
                echo "<td><div id = 'postsdiv'>" . $getRows['post'] . "</div><br>";
                echo "</tr>";
            }
         }       

        if ($var == 2)
        {
            $addpost = "INSERT INTO page_post(post,timestamps,category_id) VALUES ('$post_content','$dttime','$var')";
            mysql_query($addpost);

            $capRows = "SELECT * FROM page_post WHERE category_id = '".$var."' ORDER BY timestamps DESC";
            $iQuer = mysql_query($capRows);


            while ($getRows = mysql_fetch_array($iQuer))
            {
                echo "<tr>";
                echo "<td><div id = 'postsdiv'>" . $getRows['post'] . "</div><br>";
                echo "</tr>";
            }

        }

    }

    header("Location:Forum.php?show=true");  // <==== Note this
?>
</form>
</body>
</html>


说明:

以上代码将遵循重定向后获取模式。表单会将数据发布到同一页面,并且表单发布后您想要执行的任何任务都应该包含在

if(isset($_POST['btnPost']))
{
    ...
}

然后使用

将用户重定向到同一页面
header("Location:Forum.php?show=true");

标头功能会将用户重定向到同一页面,GET参数show将决定重定向后要显示的内容。重定向(或任何其他时间)后显示的内容应包含在

if(isset($_GET['show']))
{
    ...
}