为每个循环创建唯一的许可证ID和密码

时间:2016-04-25 08:31:07

标签: php sql forms loops for-loop

我目前正在创建一个网页,页面上的管理员可以生成特定数量的新用户(具体取决于订单)。现在,添加新用户的功能(1-many)正在运行。虽然当我创建新成员时,他们没有独特的“随机”内容。在同一生成下创建的所有用户都具有相同的。

admin.php (带有表单和函数的代码)

<head>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

  <meta charset="UTF-8">

  <title>Generera licenser</title>
<meta http-equiv="content-type" content="text/html" charset="ISO-8859-1" />



</head>



<?php
include('template.php');
if(isset($_POST['email']))            //inkluderar template och hämtar inlogg//
{
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";

function genRandomString($length = 10) {
    $characters ='0123456789abcdefghijklmnopqrstuvwxyz';
    $string = '';
    for ($p = 0; $p < $length; $p++) {
    $string .= $characters[mt_rand(0, strlen($characters))];        //funktion för slumpmässig kod//
    }
    return $string;
}
  $courseID = '1';
  $antal = $_POST['antal'];
  $role = '3'; //Detta gör att användaren som skapas får rollvärdet 3//*
  $password = genRandomString();//genererar slumpmässig kod för licens och lösenord//
  $licenseID = genRandomString();



mysql_query('SET character_set_results=utf8');
mysql_query('SET names=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');       //dessa löser åäö inmatningar till databasen.(Kan vara redundanta).
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_swedish_ci');

for( $tmp = 1; $tmp <= $antal; $tmp++){
    $query = <<<END

    INSERT INTO user(email, company, courseID, password, licenseID, role)   
    VALUES('{$_POST['email']}','{$_POST['company']}', '$courseID', '$password','$licenseID', '$role');  


END;

      //För in data från formuläret till databasen via poster.//

    $mysqli->query($query) or die(mysql_error());
}
header('Location:Bekraftat.php'); //När genereringen är genomförd skickas man till en bekräftelse sida.

}
 $content = <<<END
 <div class="row">
         <div class="container">
         <div class="jumbotronadmin">
            <div class="jumbotron">

          <div class="container">

  <h2>Generera licenser</h2>




            <form action="admin.php" method="post">

              <div class="form-group">

              <input type="text" class="form-control" aria-describedby="basic-addon1" name="email" placeholder="Email" maxlength="40" pattern="[A-Za-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" title="Ej en legitim emailadress" required>
              </div>
               <div class="form-group">
              <input type="text" class="form-control" aria-describedby="basic-addon1" name="company" placeholder="Företag" maxlength="40" patter="[A-Za-z0-9]+$" required>
              </div>
              <div class="form-group">
              <input type="text" class="form-control" aria-describedby="basic-addon1" name="antal" placeholder="Antal licenser" maxlength="3" pattern="[0-9]+$" title="OBS! Endast siffror" required>
              </div>
              <div class="form-group">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" aria-describedby="basic-addon1" name="courseID"><p>Webbutbildningen i brandskyddskunskap</p>
                    </label>
                </div>


                </div>
                <br>

              <input type="submit" class="btn btn-default" value="Generera"> 



              </form>
            </div><!-- Stänger jumbotronen --> 
            </div><!-- Stänger jumbotronadmin -->
         </div><!-- Stänger container --> 
      </div><!-- Stänger row --> 
END;

    //innehållet på admin.php med formulär//

  echo $navigation_admin;
  echo $content;
  echo $header;
    // hämtar navigationsmenyn för admin, innehåll och logotyper.//
  ?>

所以,我的问题,您是否知道如何创建一个创建唯一许可证和密码的循环?

1 个答案:

答案 0 :(得分:-1)

根据当前代码,修复是,您需要在循环内调用genRandomString函数。

for ($tmp = 1; $tmp <= $antal; $tmp++) {
    $password = genRandomString();
    $licenseID = genRandomString();
    $query = <<<END

    INSERT INTO user(email, company, courseID, password, licenseID, role)   
    VALUES('{$_POST['email']}','{$_POST['company']}', '$courseID', '$password','$licenseID', '$role');  


END;

    //För in data från formuläret till databasen via poster.//

    $mysqli->query($query) or die(mysql_error());
}

如果你想要一个非可重复的字符串,那么试试

function genRandomString($length = 10) {

    $string = md5(microtime());

    return $string;
}