Where I work we need to register computers into a database, before we just used pen an paper, but I was asked to create an easier method. So I made a small local homepage.
The computers are registered by a tag and number. The tag combined with the number must be unique, I made this loop that increment the entered number until it finds a free one.
$checkUnique = openConnection("stationer")->prepare("SELECT COUNT(1) FROM `dator` WHERE `tag_id`=:id AND `tag_number`=:number");
do{
$checkUnique -> bindParam(":number", $_POST['number']);
$checkUnique -> bindParam(":id", $_POST['tag']);
$checkUnique -> execute();
$checkUniqueResult = $checkUnique->fetchColumn();
if($checkUniqueResult != 0 && empty($searchTagNumber)){
$errors[] = "Non-unique tag and number"; break;
}
$_POST['number'] = $searchTagNumber == "+" ? $_POST['number']+1 : $_POST['number']-1;
if($_POST['number'] <= 0){
$errors[] = "The tag number can't be 0 or lower"; break;
}
}while($checkUniqueResult > 0);
But for some odd reason, it appear to randomly stop, even if the tag and number isn't unique, with no error messages, and I have no idea what causes it.