I`m making a php file to generate users and inserting them in a database. The first thing I made is to obtain the latest one and save it in a variable, then, I use a function to obtain the integer part of the string and increment that value by one, but that is the part that I think is not working.
This is the code:
<?php
$con = new mysqli('localhost', 'root', '', 'prueba');
function create_user($var){
$prefix = "U";
for($i = 0; $i < 5- strlen((String)$var); $i++) {
$prefix .= '0';
}
$var = mb_substr($var, 1);
if(is_numeric($var)) {
$int = $var++;
$var = $prefix . $int;
}
return $var;
}
$execute = mysqli_query($con, "select id_user from usuarios
WHERE id_user=(SELECT MAX(id_user) FROM usuarios)");
$row = mysqli_fetch_array($execute);
print_r($row['id_user']);
$var1 = $row['id_user'];
$userid=create_user($var1);
mysqli_query($con, "insert into usuarios (password, descripcion, id_user)
values ('A12345a', 'hgfhdgfh', ' $userid' )");
echo "hecho";
?>
Any help, please?
create_user()function correct?