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

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?






have a look at your url $_GET[var] will survive a page reload

– Bobby Axe
Sep 6 '18 at 0:14


$_GET[var]






If you don't want the $_GET['var'] parameters to be sent again then make sure to use a page redirect without attaching any parameters after the question mark ? or hash tag # in your url

– Bobby Axe
Sep 6 '18 at 0:17


$_GET['var']


?


#






https://stackoverflow.com/questions/52194794/how-to-prevent-from-reexecuting-a-query-phpmysql?fish=52194794 if you have a link like this it means that $_GET['fish'] will have the value 52194794 take notice of the question mark and the values after it

– Bobby Axe
Sep 6 '18 at 0:19


https://stackoverflow.com/questions/52194794/how-to-prevent-from-reexecuting-a-query-phpmysql?fish=52194794


$_GET['fish']


52194794






$_GET['var'] survives a page reload cuz it was displayin correct content. I've tried header('Location: ./page?id='.$_GET['id']) but now webrowser says it's loop without the end.

– Stranger
Sep 6 '18 at 0:22






yes the header(); function will redirect to the page link specified and if the page has an SQL query PHP will run it then run the header(); again and get redirected to the same page and once again run the query. SO YES ITS A LOOP

– Bobby Axe
Sep 6 '18 at 0:29


header();


header();




3 Answers
3


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


?>






Thank you, It works, kind of... cuz on that specified $_GET['id'] it stopped for executing UPDATE statement but for another link with different value of $_GET['id'] it doesn't allow to update in database for another record before timeout. Is any way to set time for this "$_SESSION['details'] = true" or something? Anyway u helped so I'm accepting this as a answer.

– Stranger
Sep 6 '18 at 1:23







you can assign any values you wish to session variables you can make more than one session variable if you wish eg $_SESSION['var1'] $_SESSION['var2'] etc and assign what ever values you wish $_SESSION['var3']=374663; //integer $_SESSION['var3']='i am a session data' //string

– Bobby Axe
Sep 6 '18 at 1:27



$_SESSION['var1']


$_SESSION['var2']


$_SESSION['var3']=374663;


$_SESSION['var3']='i am a session data'






Yeah, I know, but I mean I have one php file with one code but different content. If I click one link it show me some content at page?id=x, when I click different link it's gonna show me different content at page?id=y (this is the same code but different records from db), If I would do something with more $_SESSION vars to everything works I should every time manually type additional vars. I hope you understand me :s

– Stranger
Sep 6 '18 at 1:34






if you intend to pass multiple $_GET['values'] on the same page and use it for update query see @Obsidian Age answer as it the best approach also while using $_GET['values'] you might want to read about SQL INJECTIONS

– Bobby Axe
Sep 6 '18 at 1:35



$_GET['values']


$_GET['values']






what his answer was trying to do was check the database first if such a value as the one being inserted exists and if does DO NOT INSERT DATA but if it does not THEN INSERT DATA but in a case of update you may need to add a time stamp column or an extra column that will indicate when last the row was updated then check for that column first before update

– Bobby Axe
Sep 6 '18 at 1:41



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.


$_SESSION



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;








your answer is excellent but i feel the contributor may not be at that level yet hope you wont mind adding the solution mentioned in the comment as another method to your answer as there is no need for another answer

– Bobby Axe
Sep 6 '18 at 0:39






It doesn't work ;x Now with this additional if statement, table didnt even display.

– Stranger
Sep 6 '18 at 0:40







case and point :)

– Bobby Axe
Sep 6 '18 at 0:40



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.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế