Adding together multiple values in a foreach loop to return total

I have a table of points from an online game I am writing in PHP and I am trying to get the total of all the points where a user id matches a number in the user_id column of the points table.

I thought a way to do it was to output the values into an input field in a loop and then use jquery to add the various input values up and output the sum of all the points. But I can’t seem to get it to work.

The Query:

// GET TASKS ACTIVITY FOR PROFILES
 $query = " 
        SELECT 
            *
        FROM tasks_history
        WHERE user_id = $user_id 
        ORDER BY createdat  DESC
    "; 
     
    try 
    { 
        $stmt = $db->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query: " . $ex->getMessage()); 
    } 
         
    // Finally, we can retrieve all of the found rows into an array using fetchAll 
    $user_profile_task_feed = $stmt->fetchAll();

The frontend code:

<?php foreach($user_profile_task_feed as $user_profile_task_feed_single): ?>
    <input class="points" type="text" id="points" name="points" value="<?php echo $user_profile_task_feed_single['task_pointsearned']; ?>">
<?php endforeach; ?>