title: How to update mysqli query with random int from a button click?
body: <p>I am trying to create a program with PHP that allows the user to like(cool) or dislike(not cool) a random image from my database. When the user clicks the cool button the tabel 'image' with column 'cool' will be updated with +1. The random images are created  by the 'selectedRandomImage' function.</p>
  
  <p>The problem that I have is when I press the 'cool' button and the cool function triggers, the page reloads and random variable get's changed. So the Update query updates not the current image, but it updates the image that is displayed after pushing the 'cool' button.</p>
  
  <p>The submit button with name="cool" is on a different php page.</p>
  
  <p>Any ideas on how to fix this? 
  Feel free to give any feedback on my code because I am new and I want to improve. :-)</p>
  
  <pre><code>    &lt;form action="" method="post" class="text-center"&gt;
  
          &lt;input style="margin-right: 0 !important;" class="btn btn-success" type="submit" name="cool" value="Cool"&gt;
          &lt;input class="btn btn-danger" type="submit" name="nietcool" value="Niet Cool"&gt;
      &lt;/form&gt;
  
  
      class Cool{
      var $randomSoms;
  
      function checkHowManyImages(){
          $con = connectDB();
          $aantalImage = $con-&gt;query("SELECT COUNT(image) FROM image");
          $row = $aantalImage-&gt;fetch_row();
          return $row[0];
      }
      function selectRandom(){
          $random = rand(1, $this-&gt;checkHowManyImages());
          return $random;
      }
  
      function selectRandomImage(){
          $con = connectDB();
          $random = $this-&gt;randomSoms = $this-&gt;selectRandom(); // maakt nieuwe random aan
  
          echo "de random id in de selectRandomImage function is: " . $this-&gt;randomSoms; // check if random is the same
  
          $result = $con-&gt;query("SELECT image, name FROM image WHERE id = '$random'");
  
          while  ($row = mysqli_fetch_array($result)): ?&gt;
              &lt;div class="text-center"&gt;&lt;img src="&lt;?php echo $row["image"] ?&gt;" alt="&lt;?php echo $row["name"] ?&gt;" class="img-thumbnail "&gt;&lt;/div&gt;
          &lt;?php endwhile;
  
      }
      function cool(){
          $randomId = $this-&gt;randomSoms;
          $con = connectDB();
          $con-&gt;query("UPDATE image SET cool = cool + 1 WHERE id = '$randomId'");
      }
  
  }
  $randomimage = new Cool;
  $randomimage -&gt;selectRandomImage();
  echo "de random id in buiten de selectRandomImage function is: ".$randomimage-&gt;randomSoms; // random id
  
  // Probleem is dat er plus 1 wordt gedaan bij de volgende random omdat de pagina na button click gerefreshed wordt.:-(
  if(isset($_POST['cool'])) {
      $randomimage-&gt;cool();
  
  }
  </code></pre>