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

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