while-loop工作不正常

时间:2016-11-13 14:12:27

标签: php html mysql while-loop

我想创建一个页面,在那里我可以创建Keys,并将它们保存在数据库中,但是我遇到了一些问题。

有一个文本字段,您可以在其中写入要创建的密钥数。例如我写了5个,但它只在数据库中输入了3个,但在代码中是一个echo命令,它说他做了5次,这里是截图:

enter image description here

另外,我注意到的是,该网站在数据库中进行了最多3次托管,例如我写了10次,而他仍然只做了3次委托。
如果我只写1-3个键,则while循环正在工作。

echo "<p><a href='key.php'>Betakey administration</a></p>";

echo "<form action='#' method='POST'>";
echo "<input type='number' name='numberkeys' placeholder='Number of Keys'>";
echo "<br>";
echo "<input type='submit' name='createkey' value='Create Key'>";
echo "<input type='submit' name='keyanzeigen' value='Show all Keys'>";
echo "<input type='submit' name='unusedkeys' value='Show all unused Keys'>";
echo "<input type='submit' name='usedkeys' value='Show all used Keys'>";
echo "</form>";

$createdkeys=0;

if(isset($_POST['createkey'])){


  $minus= array(5,10,15);

  while($createdkeys<$_POST['numberkeys']){

  //creation of the keys
  for($i=1;$i<20 ;$i++){

    if(in_array($i,$minus)){
      $key=$key."-";
    }
    else{       
      $key=$key.mt_rand(0,9);
    }

    //the key looks like this: 1234-5678-1234-5678

  }


  //here it checks, if the key already exists
  $check = mysqli_query($db,"SELECT keyy FROM keyys WHERE keyy='$key'");
  $check = mysqli_fetch_array($check);

  //if the code not exists, the website will insert the key in the databank  
  //the website will echo the variable $createdkeys and increase the variable $createdkeys by one
  if(!isset($check)){
    $createdkeys= $createdkeys+1;
    echo "<p>".$createdkeys."</p>";
    mysqli_query($db,"INSERT INTO keyys VALUES('$key','NO')"); 
  }

  }
}

0 个答案:

没有答案