PHP注册系统将无法正常工作(phpmyadmin,wampserver)

时间:2017-11-29 01:13:11

标签: php mysql system

我的php注册系统将无法连接到我本地托管的phpmyadmin数据库,即使我已检查拼写错误,一切似乎都应该有效。即使在PHP注册脚本中声明了标题,标题也不会改变。没有任何东西被转移到我的数据库中(它没有错误)。如果有人能告诉我我做错了什么就会很棒。 (P.S. footer.php和header.php是正确的并包含在表格中)

注册错误处理程序并注册脚本:

<?php

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

include_once 'dbh.inc.php';

$first = mysqli_real_escape_string($conn, $_POST)$_POST['first'];
$last = mysqli_real_escape_string($conn, $_POST)$_POST['last'];
$email = mysqli_real_escape_string($conn, $_POST)$_POST['email'];
$username = mysqli_real_escape_string($conn, $_POST)$_POST['username'];
$password = mysqli_real_escape_string($conn, $_POST)$_POST['password'];

//Error handlers
//Check for empty fields
if (empty($first)) || (empty($last)) || (empty($email)) || 
(empty($username)) || (empty($password)) {
    header("Location: ../signup.php?signup=empty");
    exit();
} else {
    //Check is input characters 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=empty");
            exit();
        } else {
            $sql = "SELECT * FROM users WHERE user_username='username'";
            $result = mysqli_query($conn, $sql);
            $resultCheck = mysqli_num_rows($result);

            if ($resultCheck > 0) {
                header("Location: ../signup.php?signup=usertaken");
                exit();
            } else {
                //Hashing the password
                $hashedPassword = password_hash($password, 
PASSWORD_DEFAULT);
                //Insert the user into the database
                $sql = "INSERT INTO users (user_first, user_last, 
 user_email, user_username, user_password) VALUES ('$first', '$last', 
'$email', '$username' '$hashedPassword');";
                mysqli_query($conn, $sql);
                header("Location: ../signup.php?signup=success");
                exit();
            }
        }
    }
 }

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

数据库连接:

<?php

$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbServername = "loginsystem";

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

注册表单(php文件中的html):

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

<section class="main-container">
<div class="wrapper">
    <h2>Sign Up</h2>
    <form class="Sign" action="includes/signup.inc.php" method="POST">
        <input type="text" name="first" placeholder="First Name"><br>
        <input type="text" name="last" placeholder="Last Name"><br>
        <input type="email" name="email" placeholder="E-mail"><br>
        <input type="text" name="username" placeholder="Username"><br>
        <input type="password" name="password" placeholder="Password"><br>
        <button type="submit" name="">Sign Up!</button><br>
    </form>
</div>              
</section>  

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

如果可以,请帮忙。非常感谢。谢谢!

1 个答案:

答案 0 :(得分:0)

将名称“submit”提交给HTML注册页面的提交按钮:

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

更改PHP注册页面POST:

$first = mysqli_real_escape_string($conn, $_POST['first']);
$last = mysqli_real_escape_string($conn, $_POST['last']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);

将stmt的空值检查更改为:

if ( (empty($first)) || (empty($last)) or (empty($email)) || (empty($username)) || (empty($password)) )

如果stmt为:

,则更改输入字符有效性检查
if ( (!preg_match("/^[a-zA-Z]*$/", $first)) || ((!preg_match("/^[a-zA-Z]*$/", $last)) ) )