I am trying to make a school portal system for my coursework. Every student has a username which consists of the first letter of their name, and first 4 letters of their surname. To create this username automatically, I am using this in the page where the admins adds students.
if (isset($_POST["btnAddUser"])) {
//preparing statement to protect against sql injections
$stmt = $conn->prepare("INSERT INTO tbluser (Username, Password, Role) VALUES (?,?,?)");
$stmt->bind_param("sss", $usernamenew3, $password, $a);
$password = $_POST["s_password"];
$password = md5($password);
$name = $_POST["s_name"];
$surname = $_POST["s_surname"];
$dob = $_POST["s_dob"];
$a = "Admin";
$usernamenew = substr($name, 0, 1);
$usernamenew1 = substr($surname, 0, 4);
$usernamenew3 = $usernamenew.$usernamenew1;
However, if for example two students with the same surname, and same initial letter of their name are entered, it would come up with an error, so I need it to add 01 the first time that username is used, 02 the second time and so on. Example. Name = Test, Surname = Student For this example I would like the first username with those letters to be TStud01, the second to be TStud02...