Search code examples
phpmysqlipoints

Adding points and then show balance


I have something like this on my website. and I want to calculate the whole points of Sofia and show her the total balance. which is 163. but I am not getting how to add and then show her balance on my website. sorry, if it is too easy question. I am new to php.

here is the code

     <form method="POST">
            user name : <input type="text" name="username"><br>
            Points to Add: <input type="number" name="blance" max="100" 
     min="1">
            <br> 
            <input type="submit" name="submit">
        </form>
 <?php 

   if (isset($_POST['submit'])) {

    $servername  = "localhost";
    $user = "root";
    $password = "";
   $database = "enter code here";

   $con = mysqli_connect("$servername", "$user" , "$password" , 
 "$database");

    if ($con->connect_error) {
    die ("connection failed" . $con->connect_error);
    } else {

   $username = mysqli_real_escape_string($con, $_POST['username']);
    $blance = mysqli_real_escape_string($con, $_POST['blance']);

      $sql = "INSERT INTO user (username, blance) VALUES ('$username', 
   '$blance')";

   if ($con->query($sql) === TRUE) {
    echo "success";
  } else {
 echo "error" . $sql . "<br>" . $con->error;
 }


  $sql6 = "SELECT * FROM user";
 $result = mysqli_query($con, $sql6);
 $resultcheck = mysqli_num_rows($result);

 if ($resultcheck > 0) {
   while ($row = mysqli_fetch_assoc($result)) {
    echo $row['username'] . "<br>";
    }
 }
   echo "<br>" . "<br>" . "<br>";



    }}

  ?>

</body>

Solution

  • This query will return sum of points for each username, assuming your table is named test

    SELECT SUM(points) AS total_points
    FROM test
    GROUP BY username