Conditions : at least 1, at most 3 lines, where userid=$_SESSION['id'] ; presence of 1, 2 or 3 values for these rows in the active columns (all three are required, without repetitions).
The task is to find out whether the database has the values active = 1, 2, or 3 for these rows. If any of the values is missing, write them in order (sorting is the value of the level column in descending order), then redirect. If all values are present, make a new check on level and if the row order when sorting by lvl has changed, overwrite the values (1, 2, 3).
Trial code :
$check_main=$mysqli->query("SELECT `active` FROM `characters` WHERE `userid`='".$_SESSION['id']."' AND (`active`='1' OR `active`='2' OR `active`='3')"); // пытаемся узнать, есть ли значения 1, 2 или 3 $check_main=$check_main->fetch_assoc(); if($check_main_second==false) { // если отсутствуют, внесение $select_char=$mysqli->query("SELECT `level` FROM `characters` WHERE `userid`='".$_SESSION['id']."' ORDER BY `level` DESC LIMIT 1,1"); $select_char->fetch_assoc(); $mysqli->query("UPDATE `characters` SET `active`='1' WHERE `userid`='".$_SESSION['id']."' AND `level`='".$select_char."'"); $select_char=$mysqli->query("SELECT `level` FROM `characters` WHERE `userid`='".$_SESSION['id']."' ORDER BY `level` DESC LIMIT 2,2"); $select_char->fetch_assoc(); $mysqli->query("UPDATE `characters` SET `active`='2' WHERE `userid`='".$_SESSION['id']."' AND `level`='".$select_char."'"); $select_char=$mysqli->query("SELECT `level` FROM `characters` WHERE `userid`='".$_SESSION['id']."' ORDER BY `level` DESC LIMIT 3,3"); $select_char->fetch_assoc(); $mysqli->query("UPDATE `characters` SET `active`='3' WHERE `userid`='".$_SESSION['id']."' AND `level`='".$select_char."'"); exit(redirect("../URL")); } A terrible code that makes it impossible to convert to a string, and also there is no check if there are values. Many conditions, how to do this mind I will not attach.