刷新页面时建立数据库连接时出错

时间:2019-03-21 10:17:47

标签: php database forms mysqli connection

嗨,我正在使用PHP表单向用户使用url“ /websitename/signup.php”注册数据库时向用户发送详细信息。但是,当我运行脚本以将用户发送回页面后,一旦他们使用url“ websitename / signup.php?signup = success”进行了注册,则我收到建立数据库连接消息的错误,但详细信息已成功转移到数据库。请帮忙吗?

PHP代码

sudo log config --mode "private_data:on"

SIGNUP.PHP代码

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

  require 'connect_db.php';

    $username = $_POST['uid'];
    $email = $_POST['mail'];
    $password = $_POST['pass'];
    $passwordRepeat = $_POST['pass-repeat'];

  if (empty($username) || (empty($email) || (empty($password) || (empty($passwordRepeat))))) {
    header("Location: ../signup.php?error=emptyfields&uid=".$username."&mail=".$email);
    exit();
  }
  elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    header("Location: ../signup.php?error=invalidmail&uid=".$username);
    exit();
  }
  elseif ($password !== $passwordRepeat) {
    header("Location: ../signup.php?error=passwordcheckl&uid=".$username."&mail=".$email);
    exit();
  }
  else {

      $sql = "SELECT uidUsers FROM users WHERE uidUsers=?";
      $stmt = mysqli_stmt_init($conn);
      if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("Location: ../signup.php?error=sqlerror");
        exit();
      }
      else {
        mysqli_stmt_bind_param($stmt, "s", $username);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_store_result($stmt);
        $resultCheck = mysqli_stmt_num_rows($stmt);
        if ($resultCheck > 0) {
          header("Location: ../signup.php?error=usertaken&mail=".$email);
          exit();
        }
        else {
            $sql = "INSERT INTO users (uidUsers, emailUsers, passUsers) VALUES (?, ?, ?)";
            $stmt = mysqli_stmt_init($conn);
            if (!mysqli_stmt_prepare($stmt, $sql)) {
              header("Location: ../signup.php?error=sqlerror");
              exit();
            }
            else {
              $hashedPwd = password_hash($password, PASSWORD_DEFAULT);

              mysqli_stmt_bind_param($stmt, "sss", $username, $email, $hashedPwd);
              mysqli_stmt_execute($stmt);
              header("Location: ../signup.php?signup=success");
              exit();
            }

       }
  }

}
  mysqli_stmt_close($stmt);
  mysqli_close($conn);

  }

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

连接数据库

    <?php
include 'header.php';
 ?>

   <!-- MAIN CONTENT AREA STARTS -->

<!-- Display body section with sticky form. -->

<section>
  <div class="container signup-container">
  <h1 class="signup-header">Create an Account</h1>
  <form action="assets/includes/signup.inc.php" method="post">
    <input type="text" name="uid" placeholder="Username">
    <input type="text" name="mail" placeholder="Email">
    <input type="password" name="pass" placeholder="Password">
    <input type="password" name="pass-repeat" placeholder="Confirm Password">
    <button type="submit" name="signup-submit">Signup</button>
  </form>
  </div>
</section>

   <!-- MAIN CONTENT AREA ENDS -->

 <?php
 include 'footer.php';
 ?>

0 个答案:

没有答案