首页 > 解决方案 > 无法检查 MySQLI 数据库

问题描述

我一直在尝试制作我的登录系统一段时间,但它一直无法检查数据库或写入数据库。

注册页面:

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

<div class="header">
  <h2>Register</h2>
</div>
  
<form method="post" action="inc/signup.inc.php">

  <label>Full Name</label>
  <input type="text" name="name" placeholder="Full Name...">

  <label>Username</label>
  <input type="text" name="username" placeholder="Username">

  <label>Email</label>
  <input type="email" name="email" placeholder="Email">

  <label>Password</label>
  <input type="password" name="pwd">

  <label>Confirm password</label>
  <input type="password" name="pwdR">

  <button type="submit" name="submit">Sign Up</button>

<p>
  Already a member? <a href="login.php">Sign in</a>
</p>
</form>

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

注册后台页面

<?php

if (isset($_POST["submit"])){

    $name = $_POST["name"];
    $username = $_POST["username"];
    $email = $_POST["email"];
    $pwd = $_POST["pwd"];
    $pwdr = $_POST["pwdR"];

    require_once 'db.inc.php';
    require_once 'func.inc.php';

    if (emptyInputSignup($name, $username, $email, $pwd, $pwdr) !== False) {
        header("location: ../signup.php?error=emptyinput");
        exit();
    }
    if (invaliduid($username) !== False) {
        header("location: ../signup.php?error=invaliduid");
        exit();
    }
    if (invalidEmail($email) !== False) {
        header("location: ../signup.php?error=invalidemail");
        exit();
    }

    if (pwdMatch($pwd, $pwdr) !== False) {
        header("location: ../signup.php?error=passwordsdontmatch");
        exit();
    }
    if (uidExists($conn, $username, $email) !== False) {
        header("location: ../signup.php?error=usernametaken");
        exit();
    }

    createUser($conn, $name, $username, $email, $pwd);
}

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

现在所有功能都在下一页上处理,我认为这是我出错的地方。

<?php  

function emptyInputSignup($name, $username, $email, $pwd, $pwdr){
    $result;
    if (empty($name) || empty($username) || empty($email) || empty($pwd) || empty($pwdr)) {
        $result = true;
    }
    else{
        $result = false;
    }
    return $result;
}

function invaliduid($username){
    $result;
    if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
        $result = true;
    }
    else{
        $result = false;
    }
    return $result;
}

function invalidEmail($email){
    $result;
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $result = true;
    }
    else{
        $result = false;
    }
    return $result;
}

function pwdMatch($pwd, $pwdr){
    $result;
    if ($pwd !== $pwdr){
        $result = true;
    }
    else{
        $result = false;
    }
    return $result;
}

function uidExists($conn, $username, $email){
    $sql = "SELECT * FROM users WHERE userUid = ? OR userEmail = ?;";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)){
        header("location: ../signup.php?error=smtmfailedex");
        exit();
    }

    mysqli_stmt_bind_param($stmt, "ss" , $username, $email);
    mysqli_stmt_execute($stmt);

    $resultData = mysqli_stmt_get_result($stmt);

    if ($row = mysqli_fetch_assoc($resultData)) {
        return $row;
    }
    else{
        $result = false;
        return $result;
    }

    mysqli_stmt_close($stmt);
}

function createUser($conn, $name, $userid, $email, $pwd){
    $sql = "INSERT INTO users (userName, userUid, userEmail, userPwd) VALUES (?, ?, ?, ?);";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)){
        header("location: ../signup.php?error=stmtfailedc");
        exit();
    }
    $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);

    mysqli_stmt_bind_param($stmt, "ssss" , $name, $username, $email, $hashedPwd);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
    header("location: ../profile.php?error=none");
    exit();
}

现在我制作的数据库链接在以下页面上: DB Link

<?php

$serverName = "localhost";
$dBUsername = "root";
$dBpassword = "";
$dBname = "sakidc";

$conn = mysqli_connect($serverName, $dBUsername, $dBpassword, $dBname);

if (!$conn){
    die("Connection Failed: " . mysqli_connect_error());
}

标签: phpmysqli

解决方案


推荐阅读