0

When I run this code, the "INSERT INTO aks_balance_history" query and "UPDATE aks_account" query runs again and again.Also database is getting updated on each refresh, while the "UPDATE aks_counter" runs only once (as required). I am really stuck in this code please help.

if(isset($_POST['update_btn_counter']))
        {
            mysqli_query($link,"INSERT INTO aks_balance_history(bh_amount,bh_from,bh_to,bh_reason) VALUES('".$_POST["used_balance"]."','Counter Cash','".$_POST["select_account"]."','".$_POST["reason"]."') ");
            mysqli_query($link,"UPDATE aks_counter SET counter_balance = counter_balance - '$_POST[used_balance]' WHERE counter_id='0' ");
            mysqli_query($link,"UPDATE aks_account SET account_balance = account_balance + '$_POST[used_balance]' WHERE account_title='$_POST[select_account]' ");
        }
kingAm
  • 1,755
  • 1
  • 13
  • 23
  • 1
    Sure, that is because the browser sends the same request again. That is what you asked it to to when refreshing. If you want to prevent that, then you have to redirect the browser after having performed the database operations. You typically redirect it to some read only code that reads the result from the database and displays it. Then you can refresh how often you want. – arkascha Jun 13 '15 at 18:19
  • The problem is that the queries run again and again with browser refreshing everytime without clicking the submit button, the if condition is set with the submit button .... – Web Players Jun 13 '15 at 18:21
  • Yes. And I explained the reason for that behavior above. You do not reload the form, but the result of the form you got after the first submit. To re-deliver that result the server _must_ rerun the same code, thus the same operations are performed. What else would you expect to receive? – arkascha Jun 13 '15 at 18:22
  • What would be more feasible if we post the form to another page which has the queries that run and then it redirects back to the page OR we make functions for each query in a separate page and we call these functions here – Web Players Jun 13 '15 at 18:26
  • Sounds like a valid approach, yes. – arkascha Jun 13 '15 at 18:27
  • We have solved it by making a separate queries page ... Thanks v v much for your concern. Please reply my question so that I accept it for reputation...Thanks again – Web Players Jun 13 '15 at 18:42
  • Hence, the "Thank you for submitting" confirmation page. Consider also, clearing form fields or reset form after submitting with [javascript](http://stackoverflow.com/questions/3786694/how-to-reset-clear-form-through-javascript). By the way, the UPDATE statements do run again; they just update with same value! Finally, use [PDO prepare statements](http://www.w3schools.com/php/php_mysql_prepared_statements.asp) to avoid sql injection. – Parfait Jun 13 '15 at 18:43
  • OK, copied the comments to an answer. Great you solved your issue. Have fun! – arkascha Jun 13 '15 at 19:15

1 Answers1

1

Sure, that is because the browser sends the same request again. That is what you asked it to to when refreshing. If you want to prevent that, then you have to redirect the browser after having performed the database operations. You typically redirect it to some read only code that reads the result from the database and displays it. Then you can refresh how often you want.

Currently you do not reload the form, but the result of the form you got after the first submit. To re-deliver that result the server must re-run the same code, thus the same operations are performed.

A typical solution is to send a forms submission to some processing script and the redirect back to the form if that should be offered again.

arkascha
  • 41,620
  • 7
  • 58
  • 90