I have the following code where I enter a user, but I want to validate if the username
one I am assigning to the user already exists and if so, add a number to the username
Example: JPerez, JPerez1, JPerez2.
// Separamos los Nombres
list($pri_nombre, $seg_nombre) = explode(" ", $nombre);
// Separamos los Apellidos
list($pri_apellido, $seg_apellido) = explode(" ", $apellido);
// QUITAMOS LOS ESPACIOS EN BLANCO, EJEMPLO DE APELLIDO: DEL CAMPO
$pri_nombre = preg_replace('/[ <>\'\"]/', '', $pri_nombre);
$seg_nombre = preg_replace('/[ <>\'\"]/', '', $seg_nombre);
$pri_apellido = preg_replace('/[ <>\'\"]/', '', $pri_apellido);
$seg_apellido = preg_replace('/[ <>\'\"]/', '', $seg_apellido);
// Ticket de Condicion
$username_encontrado = FALSE;
$largo_nombre = strlen($pri_nombre); //largo del nombre
$j = 1; //INDICA LA CANTIDAD DE CARACTERES A EXTRAER DEL NOMBRE
$username = cortar_string(substr($pri_nombre, 0, $j) . $pri_apellido, 12);
while (!$username_encontrado) {
$ver_username = "SELECT Codigo FROM personal where NomUser = '$username'";
$resultt = mysqli_query($conn, $ver_username);
$resultCheck = mysqli_num_rows($resultt);
if ($resultCheck > 0) {
$username_encontrado = FALSE;
// Aqui agregar el username se repite
} else {
$username_encontrado = TRUE;
}
}
How would the code be to modify $username
it if I find one like it
I propose you to create a variable $i that carries the sequence of the numbers that you will use as suffixes in the names, every time the if is evaluated and the user already exists, this variable is increased by 1, it is concatenated with the name of the user that was initially there and the query is made again, so on until an available JPerez.$i user is found:
That option would work for you, however it seems somewhat inefficient to me, considering all the queries you have to make to the database, if for example, in the database there is from JPerez, JPerez1...until Jperez10, then when you go to sequence only up to query #12 would get an available username. Therefore, as a second alternative I propose this (although it uses PDO and not mysqli):
The advantage of this option is that it prevents SQL injections, be careful when you insert variables directly into the SQL with mysqli_query, and secondly, it doesn't do repeated queries until you get a valid user. Basically, it does the first query to check if the user exists. If it exists, it makes a second query where it extracts all the users that start with that username (so it gets all the assigned suffixes). It stores all the busy users in an array, which it uses as a reference to compare if the new calculated users are available or not.
Additionally, the variable $i does not start from zero, but from the number following the number of usernames occupied with that name, so we do not have to go through everything from the beginning, although this does not guarantee that there will be only one iteration, but if it reduces them, JPerez6 may exist, but the count result is better than 6 because JPerez5 has been deleted. Hope it's understandable:
I hope it works for you, stay tuned.