Problem with changing referring affiliates

Hello,

I’ve encountered a big problem when trying to change the referring affiliate.
If the affiliate to be reassigned is in the downline of the affiliate to whom he is to be assigned, endless loops occur and the database server crashes.
Example:
A -> B -> C
B -> D-> E -> F -> G

If you now reassign E as a sponsor of A, there is an endless loop. An error message should appear like: User E cannot become a referrer of A because E is in A’s downline. First assign a sponsor to E who is not in the same downline as A.

Please add a simple upline check before reassigning:

public function GetUserReferrer($user_id){
    $user_affiliate = $this->test_db->query("SELECT affiliate_id from ".$this->test_prefix."aff_affiliates_users where user_id =".$user_id);
    $user_affiliate = $user_affiliate->fetch_object();
    $relation = $this->test_db->query("SELECT from_affiliate_id from ".$this->test_prefix."aff_affiliates_relations where to_affiliate_id =".$user_affiliate->affiliate_id);
    $relation = $relation->fetch_object();
    if(is_object($relation)){
        $sponsor_affiliate = $this->test_db->query("SELECT user_id from ".$this->test_prefix."aff_affiliates_users where affiliate_id =".$relation->from_affiliate_id);
        $sponsor_affiliate = $sponsor_affiliate->fetch_object();
        return $this->test_db->query("SELECT * from ".$this->test_prefix."users where ID =".$sponsor_affiliate->user_id);
    }else{
            return false;
    }
}
public function ReferrerLoopCheck($user_id,$refferer_id){
    $checking = true;
    while($checking){
        $current_referrer = $this->GetUserReferrer($refferer_id);
        if (is_object($current_referrer)){
            $current_referrer = $current_referrer->fetch_object();
            if( $user_id == $current_referrer->ID || $refferer_id == $user_id){
                return true;
            }
        }else{
            $checking = false;
            return false;
        }
        $refferer_id = $current_referrer->ID;
    }
    return false;
}

Comments

One response to “Problem with changing referring affiliates”

  1. Hi there!

    Thank you very much for sharing your solution! We will verify the issue and of course release an update with appropriate modifications.

    Cheers

Share