简体   繁体   中英

how to prevent from re-executing a query php+mysql

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM