如何使用PHP增加MySQL数据库中的某些行?

I have this PHP:

$adj_index = $currentSignup + 1 - $r;//$r=3 and $currentSignup=24
for($i=1; $i<$referrals; $i++){
    $current_index = $currentSignup + 1 - $i;
    $q = "SELECT signup_id FROM app_sign_ups WHERE (adjusted_index='$current_index' AND app_id='$app_id')";
    $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q
<br />MySQL Error: " . mysqli_error($dbc));

    $next_index = $current_index + 1;
    if (mysqli_num_rows($r) == 1){
        $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
        $signuper = $row['signup_id'];
        $q = "UPDATE app_sign_ups SET adjusted_index='$next_index' WHERE (app_id='$app_id' AND signup_id='$signuper')";
        $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q
<br />MySQL Error: " . mysqli_error($dbc));
    }

}

$q = "UPDATE app_sign_ups SET adjusted_index='$adj_index' WHERE app_s_id='$app_s_id'";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q
<br />MySQL Error: " . mysqli_error($dbc));

It is supposed to update rows in my database. Each row has a value (adjusted_index) from 1 to 25 in order. With it I am taking number 25, making it 22 (by removing $r from the original number [25]) and moving the previous 22-24 up one each (so to 23, 24, 25). For some reason when I run it is resulting in 22, 24, 25, 22 instead of 23, 24, 25, 22 like I want it. I have done it on a couple other combinations and it seems that the row that has the adjusted_index that is being replaced by the last row (22 being replaced by 25).

Run only one UPDATE statement, once:

$q = " UPDATE app_sign_ups 
       SET adjusted_index = adjusted_index 
                          + CASE WHEN adjusted_index < 25
                                 THEN 1
                                 ELSE - '$r' 
                            END  
       WHERE adjusted_index >= 25 - '$r' 
         AND app_id = '$app_id'
       ORDER BY adjusted_index 
     ";

Or, even better, with 2 UPDATE statements:

$q1 = " UPDATE app_sign_ups 
        SET adjusted_index = adjusted_index + 1
        WHERE adjusted_index >= 25 - '$r' 
          AND app_id = '$app_id'
        ORDER BY adjusted_index DESC              --- this line is needed if
     ";                                           --- you have a UNIQUE index on
                                                  --- (app_id, adjusted_index)

$q2 = " UPDATE app_sign_ups 
        SET adjusted_index = 25 - '$r'
        WHERE adjusted_index = 26 
          AND app_id = '$app_id'
     ";