The site has a link mysite.ru/index.php?fun=dual , clicking on which in the database adds 1 to one value.

Simultaneously with this link in the index.php page, I display the value from the database (the value to which 1 will be added). I have written in the code that if get fun and get fun==dual exists, then 1 is added to the base, and at the same time, after clicking on the link, I bring this value out of the base, but it does not change. If you click on this link again, then +1 is already going to the database, but the old value can be seen on this page.

For example: in the database the value is 0, and on the page index.php this value is displayed, that is, 0. There is also a link at the bottom, clicking on which in the database this value is +1. So, after clicking on the link on the page, the value 0 is again visible (but if at this moment you open the database, the value will be 1). Then, if you click on this link again, then the value 1 is visible on the page (although opening the base at this moment, the value is 2), and all the time after clicking on the link there is such a difference.

How to fix this difference?

Closed due to the fact that the essence of the question is not clear by the participants user194374, aleksandr barakin , tutankhamun , Grundy , Saidolim 7 Feb '16 at 16:55 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • You would have led your code. and so first we execute update, then select and that's it - Mike
  • Mike today I can’t bring the code to the computer far, but for now I’ll use update this idea thanks for tomorrow check - Ksovrela
  • Clicking a link refreshes the page, or is an ajax request made? - Sergiks
  • Clicking on the link does not refresh the page and there is no ajax as in the first case it does not look beautiful and secondly the site for phones is Ksovrela
  • I need that after clicking on the link immediately the value is derived from the database and not the old one. I understand what can be done with ajax but I need something like on php - Ksovrela

1 answer 1

Update data first, then output it.

And it’s better to take a visitor with ?fun=dual update the database, and, without showing anything else, immediately transfer it to the same page, but without parameters. And then he will be shown an already updated counter, and the refreshment (Refresh / Reload) of the page will not wind up the bill once again.

Your index.php might look something like this:

 <?php // это самое начало. Смотрим, с чем пришёл юзер if( isset($_GET['fun']) && $_GET['fun']==='dual') { // обновляем значение в БД MySQL: UPDATE .... // и сразу редирект на эту же страницу, но без параметров header('Location: http://mysite.ru/index.php'); exit(); } // теперь получаем данные для вывода страницы // MySQL: SELECT ..... // $counter = ...; ?> <!DOCTYPE html> <html lang="Ru"> <head> <meta charset="utf-8"> <title></title> ... Счётчик: <?php echo $counter; ?>