创建多个用户的“注册表单”

时间:2016-04-13 12:32:31

标签: php html mysql sql forms

我有一个注册表单(由管理员控制),它会生成一个新用户。在表单中有一个<input>称为“金额”。

现在可以生成一个新用户,虽然我需要一个生成多个用户的功能,具体取决于填写的“金额”数量。你知道我可以使用的任何类似功能吗?

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>
  <title>Generera Licenser</title>
</head>

<?php
include('template.php');
if(isset($_POST['email']))

{
function genRandomString($length = 10) { 
    $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
    $string = '';
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }

    return $string;
}
function antal(){

}
  $antal = antal();
  $role = '3'; //Detta gör att användaren som skapas får rollvärdet 3//*
  $password= genRandomString();
  $licenseID= genRandomString();

    $query = <<<END

    INSERT INTO user(email, company, courseID, antal, password, licenseID, role)

    VALUES('{$_POST['email']}','{$_POST['company']}','{$_POST['courseID']}', '$antal', '$password','$licenseID', '$role');
END;
    $mysqli->query($query);


echo 'Nya licenser har lagts till i databasen';

}
 $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" action="sendback.php" method="post">
              <div class="form-group">
              <input type="text" class="form-control" aria-describedby="basic-addon1" name="company" placeholder="Företag" maxlength="40">
              </div>
              <div class="form-group">
              <input type="text" class="form-control" aria-describedby="basic-addon1" name="email" placeholder="Email" maxlength="40">
              </div>
              <div class="form-group">
              <input type="text" class="form-control" aria-describedby="basic-addon1" name="antal" placeholder="Antal licenser" maxlength="3">
              </div>
              <div class="form-group">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" aria-describedby="basic-addon1" name="courseID">Webbutbildningen i Allmän brandskyddskunskap
                    </label>
                </div>

                 <div class="checkbox">
                    <label>
                        <input type="checkbox" aria-describedby="basic-addon1" name="role">Skapa en ny administrativ användare
                    </label>
                </div>
                </div>
              <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;
  echo $navigation_admin;
  echo $content;
  echo $header;
  ?>

1 个答案:

答案 0 :(得分:0)

您可以循环查询,如下所示:

假设获得计数$amount = $_POST['amount'];

for($i=0; i<$amount; $i++){
    $query = <<<END

    INSERT INTO user(email, company, courseID, antal, password, licenseID, role)

    VALUES('{$_POST['email']}','{$_POST['company']}','{$_POST['courseID']}', '$antal', '$password','$licenseID', '$role');
END;
    $mysqli->query($query);
}
  

Your script is at risk for SQL Injection Attacks。了解MySQLi的prepared statements。即使逃避字符串也不安全!