简体   繁体   English

在do-while循环中选择计数中断

[英]Select count breaks in do-while loop

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. 但是出于某种奇怪的原因,即使标签和编号不是唯一的,也没有错误消息,并且它也不知道是什么原因造成的,所以它似乎会随机停止。

You can use an unique key like this: 您可以使用像这样的唯一键

UNIQUE KEY `computer_key` (`tag`,`number`)

Or change engine MyISAM: mysql two column primary key with auto-increment 或更改引擎MyISAM: 带有自动增量的mysql两列主键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM