即使给出成功消息,数据也不会插入数据库

时间:2018-12-29 20:26:27

标签: php mysql sql-insert

我正在使用数据库进行注册项目,并且正在使用php将数据插入数据库中。我应该遇到一个错误,该错误不会让我的程序将数据插入到我的数据库中,而是通过将程序发送到成功页面来成功完成。

在这里附上我的注册页面:

 <?php
include_once 'header.php';
 ?>
<section class="main-container">
      <div class="main-wrapper">
        <h2>Signup</h2>
        <form class="signup-form" action="includes/signup.inc.php" method="POST">
          <input type="text" name="first" placeholder="First Name">
          <input type="text" name="last" placeholder="Last Name">
          <input type="text" name="email" placeholder="E-mail">
          <input type="text" name="uid" placeholder="Username">
          <input type="password" name="pwd" placeholder="Password">
          <button type="submit" name="submit">Sign up</button>
        </form>
      </div>
</section>
<?php
  include_once 'footer.php'
?>

这是我的联系:

<?php
  $dbServername = "localhost";
  $dbUsername = "root";
  $dbPassword = "root";
  $dbName = "loginsystem2";

  $conn = mysqli_connect($dbServername, $dbUsername,$dbPassword,$dbName);


 ?>

这是我插入数据库的信息:

 <?php
  if (isset($_POST['submit'])) {

      include_once 'dbh.inc.php';

      $first = mysqli_real_escape_string($conn, $_POST['first']);
      $last = mysqli_real_escape_string($conn, $_POST['last']);
      $email = mysqli_real_escape_string($conn, $_POST['email']);
      $uid = mysqli_real_escape_string($conn, $_POST['uid']);
      $pwd = mysqli_real_escape_string($conn, $_POST['pwd']);


      //Error handlers
      //Check for empty fields
      if (empty($first) || empty($last) || empty($email) || empty($uid) || empty($pwd)) {
        header("Location: ../signup.php?signup=empty");
        exit();
      } else {
        //Check if input character are valid
        if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
            header("Location: ../signup.php?signup=invalid");
            exit();
        } else {
          //Check if email is valid
          if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            header("Location: ../signup.php?signup=email");
            exit();
          } else {
            $sql = "SELECT * FROM users WHERE user_uid = '$uid'";
            $result = mysqli_query($conn, $sql);
            $resultCheck = mysqli_num_rows($result);

            if ($resultCheck > 0) {
                header("Location: ../signup.php?signup=usertaken");
                exit();
            } else {
                //Hashing the Password
                $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
                //Insert the user into the database
                $sql = "INSERT INTO users (user_first, user_last, user_email, user_uid, user_pwd)
                VALUES ('$first', '$last', '$email', '$uid', '$hashedPwd');";
                mysqli_query($conn, $sql);

                header("Location: ../signup.php?signup=success");
                exit();

            }
          }
        }
      }

  } else {
    header("Location: ../signup.php");
    exit();
  }


 ?>

0 个答案:

没有答案