Running total in PHP Code (online php coding)

Hello! I am making a web app/program using html, css and php. I know basics in html, css and php, but still dont know much, especialy in php. The program i am making keeps score for a game, so the website opens, you enter how many points where earned, and the program tells you what the current score is. I have everything worked out, exept when you re-enter the next amount of points earned, it doesnt add it to the old, it just displays the new amount. HOW DO I KEEP A RUNNING TOTAL?? Below Is The PHP Code Used… And A Link To The Website Where It Is Hosted.

PHP CODE-

<?php
$bonus= $_POST['bonus'];
$buzz= 10;
$total= $bonus + $buzz;
$runningtotal= $total + $runningtotal;

echo "points gotten this round: $total points total: $runningtotal ";
?>

LINK-
www.allthingsjacob.com/bb/index1.html
the link brings you to the html, after entering first number it forwards you to the php code (allthingsjacob.com/bb/work.php) which procceses the code, then after the php closing tag (?>) the same html code is run. below is every single line of code on the work.php page-

<?php
$bonus= $_POST['bonus'];
$buzz= 10;
$total= $bonus + $buzz;
$runningtotal= $total + $runningtotal;

echo "points gotten this round: $total points total: $runningtotal ";
?>
<some tags not here so that this website shows the code and not the final product.>
  <head f>
    <titled>BBS</title>
  </head>
  
  <Taken so you can see the code>
    <h1>
      Bible Bowl Score-Keeper Web Application Designed By Jacob H
    </h1>
    <p>
      Version 0.0
    </p>
    <h3>
      Select Amount Of Gotten Bonus Points. The App Will Do The Rest. 
    </h3>
    <form action="work.php" method="post" value="bbs" name="bonuspoints">
      <!-- Below Is Radio Buttons For The Selection Of Bonus Points Gotten. PHP work.php Adds 10. -->
      <input type="text" name="bonus">
     
      <input type="submit">
      
    </form>
  <Taken so you can see the code>
<Taken so you can see the code>

Thanks For Your Help!!

If you want to keep a record of something without a database, then try using sessions. Though I am not sure how long you want to keep their records so I’d suggest using a database. Don’t use mysql_* as it has been removed from PHP 5.6 and the current PHP 7.0.x. Use PDO or mysqli_*. Also, you should check whether the form has been submitted because you will most likely get an undefined index if someone accesses that PHP file directly. Proper way is by using if($_SERVER['REQUEST_METHOD'] == 'POST'). Don’t use isset($_POST[...]) as this is an amateur “hack” and is flawed and is only taught in legacy code tutorials.

Every time you run your PHP code, it’s run from scratch on the server, and when the program ends all values are lost. The only value that will be present is $bonus (because you get it from the form post value), and $buzz because you define it in the code. $runningtotal will be set to the same as $total, because as soon as your code ends, all the variables are lost and $runningtotal, like any undefined variable, will start with a value of zero.

If you want to keep a running total like that, you’ll either have to put it in a database or use session variables as @spaceshiptrooper said above.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.