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;
     }
    }
 ?>

```
You need to add in some sort of logic to ensure that the MySQL only triggers once, and make this conditional logic persist through page reloads. There are a number of ways of doing this (such as with cookies or `$_SESSION` flags, but I would personally recommend querying against the database. This approach will ensure that the code can only be run once, no matter which user on which machine is trying to run it.

This is illustrated in the following pseudo-code:

```
mysqli_query($conn, "SELECT something WHERE id = " . $_GET['id']);
if(mysqli_num_rows($result) === 0) { /* Check that the code hasn't run yet */
    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;
        }
    }
}

```