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

I'm working with our project and I noticed that whenever I refresh my page the mysql query repeats itself. When I click a submit button It will go to same page and it will perform the query. Even though i used isset() method to the submitting button, still the query repeats when I refresh/reload the page. Thank you :) !

<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>

If you refresh a page after submitting you form, the POST will still be recognised by some browsers and will cause a second POST to the code. You should update your code to trigger the SQL query on a separate page or function, and then redirect the user to the success / thanks page, where refreshing won't duplicate the query.

Alternatively, you can have a hidden field on your page which contains a unique token and compare it with a cookie. On page load, you save the token to a cookie and to the hidden field on the form. When you submit the form, validate that the token in the hidden form field matches the cookie, then delete the cookie. Refreshing the page after submission will cause the token validation to fail, preventing a duplicate SQL insert.

Just wash out the form data by redirecting the page after insert query like header('location:home.php')

As @PeeHaa suggested in the comments above use Post-Redirect-Get concept.
Modified your code a bit. Try below:

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>


Explanation:

The above code will follow the Post-Redirect-Get pattern. The form will post the data to the same page and whatever task you want to perform after form post should be enclosed in,

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

and then redirect the user to the same page using,

header("Location:Forum.php?show=true");

the header function will redirect the user to the same page and the GET parameter show will decide what to show after the redirection. The content to show after redirection (or any other time) should be enclosed in,

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