Question: I'm learning a PHP and MySQL.
I decided to play a little and I made a dynamic -ish page which I implemented with MySQL and if loop (I don't care if it's worth or not, just for fun). 

Code looks like this example:

```
mysqli_query($conn,"UPDATE something SET something = something + 1 WHERE id = " . $_GET['id']

   if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
  echo table;
    }
    }

```

And every time someone clicks a link at a sub page it passing $\_GET['id'] to this code above. And everything works ok but... but after refreshing the page a query with UPDATE statement is "re-executing" (I don't know if it's correct - English isn't my native language) and header('Location: ') doesn't work... its showing just "Page cannot found".

Is there any way to prevent this re-executing query after refreshing?

Answer: 
```
<?php
    session_start(); //you call this to use session variables

   if(isset($_SESSION['details']) && $_SESSION['details']==true)
   { /*do nothing since query has ran once*/}
   else
   {
    /*run query since query has not been run*/
    mysqli_query($conn,"UPDATE something SET something = something + 1 WHERE id = " .$_GET['id'];
     $_SESSION['details']=true; //you set a session variable to true when query runs the first time.
    }

    /*i am assuming there is a select statement here that retruns the **$result** */
    if(mysqli_num_rows($result) > 0)
    {
     while($row = mysqli_fetch_assoc($result))
     {
      echo table;
     }
    }
 ?>

```
An often used method to prevent a page refresh repeating an action (database or otherwise) is to redirect to another page to show the results.

The user clicks link or submits a form etc, which goes to a page that does the job. That page when done, sends a redirect header to a page that will show the outcome of the action. If the user refreshes, they just get the results page again.

$\_SESSION is usually used to get the results from the action page to the results page, or the results page may run it's own 'SELECT' query.

Another common method is using AJAX to call the page doing the action and return the results. In this case the current page remains the same, and refreshing will not cause the page doing the action to run again.