Add user star function triggering PHPmailer email optimisation The function below simple allows a logged in user to "star" another user for convenience when trying to find the that user another time. At the moment the function works as it should, it inserts the user giving the star and the user receiving the star into a database table, then grabs the first name and email from the users table to get more information about the user receiving the star and finally uses that data to send an email to the user receiving the star.

As it stands however, this function is pretty slow when used. Based on user research, users tend to press the "Star user" button numerous times as the application seems unresponsive; when in reality, it is just taking longer as it is sending an email (I've tested the function without the email trigger and it is much faster).

Is there a better way of triggering emails to increase performance using this as an example? Any improvements/recommendations would also be appreciated. 

public function addstar($user_id, $starredBy) {

    $query = $this-&gt;db-&gt;prepare("INSERT INTO " . DB_NAME . ".user_stars (star_id, starred_by_id) VALUES (?, ?)");

    $query-&gt;bindValue(1, $user_id);
    $query-&gt;bindValue(2, $starredBy);

    $this-&gt;db-&gt;beginTransaction();

    try{
        $query-&gt;execute();

        $getUserInfo = $this-&gt;db-&gt;prepare("SELECT u.firstname, u.email, u.user_id FROM " . DB_NAME . ".users AS u WHERE u.user_id = ?");
        $getUserInfo-&gt;bindValue(1, $user_id);
        $getUserInfo-&gt;execute();
        $to = $getUserInfo-&gt;fetch();

        $firstName = $to['firstname'];
        $email = $to['email'];

        $this-&gt;emails-&gt;sendStarEmail($firstName, $email);

        $this-&gt;db-&gt;commit();

        header('Location: ' . $_SERVER['HTTP_REFERER']);

    }catch(PDOException $e) {
        $this-&gt;db-&gt;rollback();
        $users = new Users($db);
        $debug = new Errors();
        $debug-&gt;errorView($users, $e);    
    }
}
Lock the UI until a response comes back (or a reasonable timeout
occurs) to prevent the multiple clicking.
Don't send emails directly from the code, push them to spool or MQ and process separately


As for the code itself - there's nothing much to comment except that it's completely out of touch with modern best practices in PHP world. Consider using a proper framework, ORM or at least a DBAL. See phptherightway for more details.