I have a piece of mysqli code which I written where it will insert values into the database:
$insertsql = "
INSERT INTO Teacher
(TeacherId, TeacherForename, TeacherSurname, TeacherEmail, TeacherAlias, TeacherUsername, TeacherPassword, Active, Code)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
// Handle errors with prepare operation here
}
$teacherid = ;
$insert->bind_param("sssssssss", $teacherid, $getfirstname, $getsurname,
$getemail, $getteachid, $getuser,
$password, $active, $code);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
But I have a little problem. At the moment I have left the $teacherid variable blank, but what I want this variable to do it to find the last “TeacherId” from the database and insert a new one by inserting the next “TeacherId”.
FOR EXAMPLE:
If the “Teacher” Table looks like this for TeacherId:
TeacherId
T1
T2
T3
T4
Then when I Insert a new TeacherId value, it should insert T5, this is because T4 is the current highest T number so the next number should be T5.
Does anyone know how this can be achieved. Any help would be much appreciated 🙂
Thanks
Change
TeacherIdfrom avarcharto anint. The create table will look a bit like this:The
auto_incrementwill start from 1 and automatically increase by 1 on each insert.If you must prepend the
T, you can always do it withCONCATor in the php code.