Php Mysql-不插入多行

Php Mysql-不插入多行,php,html,mysql,Php,Html,Mysql,我已经创建了一个注册和登录页面。它第一次运行良好,我在注册表格中输入的数据被插入到我的数据库中。但是当我第二次尝试时,mysql没有将数据输入表中。我试了几次,但表中只显示了一行,没有插入更多的行 这是将数据插入数据库的代码 <?php session_start(); // initializing variables $username = ""; $email = ""; $errors = array(); // connect to the data base $db

我已经创建了一个注册和登录页面。它第一次运行良好,我在注册表格中输入的数据被插入到我的数据库中。但是当我第二次尝试时,mysql没有将数据输入表中。我试了几次,但表中只显示了一行,没有插入更多的行

这是将数据插入数据库的代码

<?php
session_start();

// initializing variables
$username = "";
$email    = "";
$errors = array();

// connect to the data base
$db = mysqli_connect('localhost', 'root', '', 'registration');


// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);

// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); 
 }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
 }

// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);

if ($user) { // if user exists
if ($user['username'] === $username) {
  array_push($errors, "Username already exists");
}

if ($user['email'] === $email) {
  array_push($errors, "email already exists");
}
}

/ / Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database

$query = "INSERT INTO users (username, email, password)
          VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: profile.php');
 }
}

// LOGIN USER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);

if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}

if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
  $_SESSION['username'] = $username;
  $_SESSION['success'] = "You are now logged in";
  header('location: profile.php');
}else {
    array_push($errors, "Wrong username/password combination");
}
 }
}

?>

正如我在评论中已经指出的,像这样的问题,一行插入很好,而下一行通常与
-您的主键行没有自动递增,这会导致重复的主键和新插入被拒绝(这里是问题所在)
-或者将不应该是唯一的行定义为唯一(例如,将密码行设置为唯一是个坏主意)

它和PHP无关,但需要在数据库中修复。如果使用PHPMyAdmin,则选中主键的a_i复选框,并确保应该有索引但不应该是唯一的或主键设置为索引的行


在普通SQL中,使用
AUTO_INCREMENT
关键字,如
createtablewhatever(ID INT(16)主键notnull AUTO_INCREMENT)。至于在MySQL中创建适当的索引,请查看关键字unique,如果它会造成麻烦,请避免使用它。

首先有两件显而易见的事情:您容易受到sql注入的攻击,因此请使用预先准备好的语句!请不要使用md5进行密码散列。md5不够安全。PHP有内置的安全密码处理方法
password\u hash
password\u verfiy
。没有,但很多人仍然没有意识到这一点,因此提醒他们,而不是让他们生活在虚假的安全感中,总是一件好事。我还在查你的密码。你有任何错误吗?嗯,我不知道你是否编写了此代码,但你确定你没有使用相同的用户名/电子邮件进行第二次注册吗?原因此代码检查。或者这两个密码不匹配?这看起来类似于我的一个数据库出现的问题,与PHP无关(正如您已经注意到的,它在第一次尝试时就可以工作,而不是之后),而是与您如何定义数据库有关。我可以想到两个问题:1)您定义的主键不是自动递增(重复问题)。2) 不应为唯一的行被设置为唯一。例如,密码行。@vigneshu不,不是。有一些方法可以通过escape函数,所以最好的安全方法是使用准备好的语句。
<?php include('server.php') ?>

 ///...

  <body>
  <div class="form">

  <ul class="tab-group">
    <li class="tab active"><a href="#signup">Sign Up</a></li>
    <li class="tab"><a href="#login">Log In</a></li>
  </ul>

  <div class="tab-content">
    <div id="signup">
      <h1>Sign Up for Free</h1>

      <form action="register.php" method="post">
        <?php include('errors.php'); ?>
        <div class="field-wrap">
          <label>
            User Name<span class="req">*</span>
          </label>
          <input type="text" name="username" value="<?php echo $username; ?>"required autocomplete="off" />
        </div>



      <div class="field-wrap">
        <label>
          Email Address<span class="req">*</span>
        </label>
        <input type="email" name="email" value="<?php echo $email; ?>" required autocomplete="off"/>
      </div>

      <div class="field-wrap">
        <label>
          Set A Password<span class="req">*</span>
        </label>
        <input type="password" name="password_1" required autocomplete="off"/>
      </div>
      <div class="field-wrap">
        <label>
          Set A Password Again<span class="req">*</span>
        </label>
        <input type="password" name="password_2" required autocomplete="off"/>
      </div>

      <button type="submit" class="button button-block" name="reg_user"/>Get Started</button>

      </form>

    </div>

    <div id="login">
      <h1>Welcome Back!</h1>

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

       <div class="field-wrap">
        <label>
          User name<span class="req">*</span>
        </label>
        <input type="text" name="username" required autocomplete="off"/>
       </div>

      <div class="field-wrap">
        <label>
          Password<span class="req">*</span>
        </label>
        <input type="password" name="password"required autocomplete="off"/>
      </div>


      <button class="button button-block" name="login_user"/>Log In</button>

      </form>

    </div>

  </div><!-- tab-content -->

</div> <!-- /form -->

 <script  src="register.js"></script>

  </body>
</html>