How can PHP developers optimize the code provided to efficiently handle point allocation for users upon login?

Issue: To efficiently handle point allocation for users upon login, PHP developers can optimize the code by using a database query to retrieve the user's current points, updating the points based on any criteria, and then saving the updated points back to the database.

// Retrieve user's current points from the database
$user_id = $_SESSION['user_id'];
$query = "SELECT points FROM users WHERE id = $user_id";
$result = mysqli_query($connection, $query);
$user = mysqli_fetch_assoc($result);
$current_points = $user['points'];

// Update points based on any criteria
$new_points = $current_points + 10; // For example, add 10 points upon login

// Save the updated points back to the database
$update_query = "UPDATE users SET points = $new_points WHERE id = $user_id";
mysqli_query($connection, $update_query);