php – payout for selected user

You need to create a simple payout page where you can select a user from DB.

In the & # 39; user's & # 39; table have: total_earn, total_payout, balance

Select a user from the drop-down menu

query($user);

    if($result_user->num_rows > 0) {
        while($row = $result_user->fetch_assoc()) {
            $user_id = $row('id');
            $username = $row('username');
            $balance = $row('balance');
            $payout = $row('total_payout');
?>                              
    

Update DB

connect_error);
    }

    // Escape user inputs for security
    $user_id    = $mysqli->real_escape_string($_REQUEST('user_id'));
    $value      = $mysqli->real_escape_string($_REQUEST('value'));

    // Attempt insert query execution
    $sql = "INSERT INTO `payment` (`user_id`, `value`) VALUES ('$user_id', '$value')";

    if($mysqli->query($sql) === true) {
        $new_balance = $balance - $value;
        $total_payout = $payout + $value;

        $sql_balance = "UPDATE `users` SET balance = '$new_balance', total_payout = '$total_payout' WHERE id = '$user_id'";
        if($mysqli->query($sql_balance) === true) {
            $_SESSION('success_message') = "Successfully payout.";
            header("location: payout.php");
            exit();
        }
    } else {
        $_SESSION('error_message') = "ERROR: Could not able to execute $sql. " . $mysqli->error;
        header("location: login.php");
        exit();
    }

    // Close connection
    $mysqli->close();
}

include ("includes/footer.php"); 

For example, in DB total_earn = 20, total_payout = 0, balance = 20. If I enter 10 and select a user and click submit, in DB update total_payout to 10, but balance goes to -10.