检查密码重复时,PHP注册表单无法正常运行

时间:2019-02-13 16:36:52

标签: php

当我尝试测试注册错误时,除了密码/密码重复出现,所有其他错误都起作用,就像我尝试将其提交到数据库一样,该页面显示了错误

  

注意:未定义变量:stmt输入   C:\ xampp \ htdocs \ Presence \ includes \ signup.inc.php在第85行

     

警告:mysqli_stmt_close()期望参数1为mysqli_stmt,   C:\ xampp \ htdocs \ Presence \ includes \ signup.inc.php中给出的null   85

我的注册PHP页面代码

<?php
    require "PresenceNavbar.php"
?>
<link href="css/SignUp.css" rel="stylesheet" type="text/css">
<body scroll="no" style="overflow: hidden">
    <main>
        <div>
            <section class ="SignupSheet">
                <img src="assets/Keyhole.png" width="130" class="avatar">
            <h1>Sign Up</h1>

                <?php
                    if (isset($_GET['error']))
                    {
                        if ($_GET['error'] == "emptyfields")
                        {
                            echo '<p class = "SS" id="SS">Fill in all fields!</p>';
                        }

                        else if ($_GET['error'] == "invaliduidmail")
                        {
                            echo '<p class = "SS" id="SS">Invalid username and e-mail!</p>';
                        }

                        else if ($_GET['error'] == "invaliduid")
                        {
                            echo '<p class = "SS" id="SS">Invalid username!</p>';
                        }

                        else if ($_GET['error'] == "invalidmail")
                        {
                            echo '<p class = "SS" id="SS">Invalid e-mail!</p>';
                        }

                        else if ($_GET['error'] == "passwordcheck")
                        {
                            echo '<p class = "SS" id="SS">Your passwords do not match!</p>';
                        }

                        else if ($_GET['error'] == "usertaken")
                        {
                            echo '<p class = "SS" id="SS">Username is already taken!</p>';
                        }

                    }

                ?>


            <form class="Ssheet" action="includes/signup.inc.php" method="post">
                <p>Username</p>
                <input type="text" name="uid" placeholder="Username">
                <p>E-mail</p>
                <input type="text" name="mail" placeholder="E-mail">
                <p>Password</p>
                <input type="password" name="pwd" placeholder="Password">
                <p>Repeat Password</p>
                <input type="password" name="pwd-repeat" placeholder="Repeat Password">
                <input type="submit" name="signup-submit" id="signup-submit" value="Sign Up">
            </form>
            </section>
        </div>

    </main>
</body>

我的Signup.inc.php文件

<?php

if (isset($_POST['signup-submit']))
{
    require 'dbh.inc.php';


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

    if (empty($username) || empty($email) || empty($password) || empty($passwordRepeat))
    {

        header("Location: ../PresenceSignup.php?error=emptyfields&uid=".$username."&mail=".$email);
        exit();
    }

    else if (!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9]*$/", $username))
    {
        header("Location: ../PresenceSignup.php?error=invalidmailuid");
    }

    else if (!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        header("Location: ../PresenceSignup.php?error=invalidmail&uid=".$username);
        exit();
    }

    else if (!preg_match("/^[a-zA-Z0-9]*$/", $username))
    {
        header("Location: ../PresenceSignup.php?error=invaliduid&mail=".$email);
        exit();
    }
    else if ($password !== $passwordRepeat)
    {
        ("Location: ../PresenceSignup.php?error=passwordcheck&uid=".$username."&mail=".$email);
    }
    else
    {

        $sql = "SELECT uidUsers FROM users WHERE uidUsers=?";
        $stmt = mysqli_stmt_init($conn);

        if (!mysqli_stmt_prepare($stmt, $sql))
        {
            header("Location: ../PresenceSignup.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: ../PresenceSignup.php?error=usertaken&mail=".$email);
                exit();
            }
            else 
                {
                $sql = "INSERT INTO users (uidUsers, emailUsers, pwdUsers) VALUES (?, ?, ?)";
                $stmt = mysqli_stmt_init($conn);
                if (!mysqli_stmt_prepare($stmt, $sql))
                {
                    header("Location: ../PresenceSignup.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: ../PresenceLogin.php");
                    exit(); 

                }
            }
        }   

    }
    mysqli_stmt_close($stmt);
    mysqli_close($conn);
}
else
{
    header("Location: ../PresenceSignup.php");
    exit();
}

修改 我移动了mysqli_stmt_close($ stmt);和mysqli_close($ conn);到正确的位置,但是现在它将我重定向到带有空白页面的login.inc.php文件,而不是停留在同一页面上并给我错误输出

1 个答案:

答案 0 :(得分:0)

更改这部分代码

 else
        {

            $sql = "SELECT uidUsers FROM users WHERE uidUsers=?";
            $stmt = mysqli_stmt_init($conn);

            if (!mysqli_stmt_prepare($stmt, $sql))
            {
                header("Location: ../PresenceSignup.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: ../PresenceSignup.php?error=usertaken&mail=".$email);
                    exit();
                }
                else 
                    {
                    $sql = "INSERT INTO users (uidUsers, emailUsers, pwdUsers) VALUES (?, ?, ?)";
                    $stmt = mysqli_stmt_init($conn);
                    if (!mysqli_stmt_prepare($stmt, $sql))
                    {
                        header("Location: ../PresenceSignup.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: ../PresenceLogin.php");
                        exit(); 

                    }
                }
            }   

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

您的$ smtp变量声明与您所调用的范围不在同一范围内,它必须处于ELSE条件内