我正在尝试使用验证创建此登录/注册表单

时间:2018-04-15 20:59:14

标签: javascript php html css mysql

我的代码中出现了某种php错误,但我不确定它是什么  当你到达底层的PHP时,我的代码开始变得有趣。我试图验证用户名和密码,但我得到一些类型的错误

   <?php 
        include 'config.php';

    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Register Page</title>
    <link rel="stylesheet" href="style.css">
    </head>
    <body>

        <div id="main-wrapper">
            <center>
                <h2 style="color: white;">Register Page</h2>
            <img class="avatar" src="spaceman2.jpg" style="height: 100px; width: 100px" />

            </center>

            <form class="my-form" action="register.php" method="post">
                <label><b>Username<b></label>
                <input name="username" type="text" class="input-values" placeholder="Your username" required/>

                <label><b>Password<b></label>
                <input name="password" type="password" class="input-values" placeholder="Your password" required/>

                <label><b>Confirm Password<b></label>
                <input name="cpassword" type="password" class="input-values" placeholder="Confirm password" required/>

                <input name="signup_btn" id="signup-btn" type="submit" value="Sign Up"/>
                <br/>
                <a href="login.php"><input id="back-btn"  type="button" value="<- Back"/></a>

            </form>


    <?php

                if(isset($_POST['signup_btn']))
                {
                    //echo '<script type="text/javascript"> alert("You are now signed in!")</script>';

                    $username = $_POST['username'];
                    $password = $_POST['password'];
                    $cpassword = $_POST['password'];

                    if($password == $cpassword)
                    {
                        $query= "SELECT * FROM user WHERE username ='$username'";

                        $query_run = mysqli_query($con,$query);

                        if(mysqli_num_rows($query_run)>0)
                        {
                            echo '<script type="text/javascript"> alert("Astronaut name already exist") </script>';

                        }
                        {
                            $query= "insert into user values('$username','$password')";
                            $query_run = mysqli_query($con, $query);

                            if($query_run)
                            {
                                echo '<script type="text/javascript"> alert("Astronaut is now registered! Go to Login Page!") </script>';
                            }
                            else 
                            {
                                echo '<script type="text/javascript"> alert("Error!") </script>';
                            }
                        }


            ?>

3 个答案:

答案 0 :(得分:0)

一些事情:

您的$ cpassword是引用您的$ _POST ['密码']而不是cpassword。

您的插入语句缺少VALUES之前的列引用。

您还应该考虑使用密码哈希/加密。

答案 1 :(得分:0)

由于

,您的代码出错了

特别在php

您忘记了else,并且password使用了$cpasswrod

html中,您不会关闭</b>

<?php
                include 'config.php';
                if(isset($_POST['signup_btn']))
                {
                    //echo '<script type="text/javascript"> alert("You are now signed in!")</script>';

                    $username = $_POST['username'];
                    $password = $_POST['password'];
                    $cpassword = $_POST['cpassword'];

                    if($password == $cpassword){
                        $query= "SELECT * FROM user WHERE username ='$username'";

                        $query_run = mysqli_query($con,$query);

                        if(mysqli_num_rows($query_run)>0)
                        {
                            echo '<script type="text/javascript"> alert("Astronaut name already exist") </script>';

                        }
                        else{
                            $query= "insert into user values('$username','$password')";
                            $query_run = mysqli_query($con, $query);

                            if($query_run)
                            {
                                echo '<script type="text/javascript"> alert("Astronaut is now registered! Go to Login Page!") </script>';
                            }
                            else 
                            {
                                echo '<script type="text/javascript"> alert("Error!") </script>';
                            }
                        }

                    }
                }
            ?>
    <html>
    <head>
        <title>Register Page</title>
    <link rel="stylesheet" href="style.css" />
    </head>
    <body>

        <div id="main-wrapper">
            <center>
                <h2 style="color: white;">Register Page</h2>
            <img class="avatar" src="spaceman2.jpg" style="height: 100px; width: 100px" />

            </center>

            <form class="my-form" action="index.php" method="post">
                <label><b>Username</b></label>
                <input name="username" type="text" class="input-values" placeholder="Your username" required/>

                <label><b>Password</b></label>
                <input name="password" type="password" class="input-values" placeholder="Your password" required/>

                <label><b>Confirm Password</b></label>
                <input name="cpassword" type="password" class="input-values" placeholder="Confirm password" required/>

                <input name="signup_btn" id="signup-btn" type="submit" value="Sign Up"/>
                <br/>
                <a href="login.php"><input id="back-btn"  type="button" value="<- Back"/></a>

            </form>

        </div>
        </body>
</html>

我希望它有所帮助

答案 2 :(得分:0)

以下是您的代码的新版本:(试用)

<?php 
    include 'config.php';
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Register Page</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div id="main-wrapper">
            <center>
                <h2 style="color: white;">Register Page</h2>
                <img class="avatar" src="spaceman2.jpg" style="height: 100px; width: 100px" />
            </center>
            <form class="my-form" action="register.php" method="post">
                <label><b>Username</b></label>
                <input name="username" type="text" class="input-values" placeholder="Your username" required />

                <label><b>Password</b></label>
                <input name="password" type="password" class="input-values" placeholder="Your password" required />

                <label><b>Confirm Password</b></label>
                <input name="cpassword" type="password" class="input-values" placeholder="Confirm password" required />

                <input name="signup_btn" id="signup-btn" type="submit" value="Sign Up"/>
                <br/>
                <a href="login.php"><input id="back-btn"  type="button" value="<- Back"/></a>
            </form>
            <?php

            if(isset($_POST['signup_btn']))
            {
                //echo '<script type="text/javascript"> alert("You are now signed in!")</script>';

                $username = $_POST['username'];
                $password = $_POST['password'];
                $cpassword = $_POST['cpassword'];

                if($password == $cpassword)
                {
                    $query= "SELECT * FROM user WHERE username ='$username'";

                    $query_run = mysqli_query($con,$query);

                    if(mysqli_num_rows($query_run)>0)
                    {
                        echo '<script type="text/javascript"> alert("Astronaut name already exist") </script>';

                    }else
                    {
                        $query= "insert into user (username, password) values('$username','$password')";
                        $query_run = mysqli_query($con, $query);

                        if($query_run)
                        {
                            echo '<script type="text/javascript"> alert("Astronaut is now registered! Go to Login Page!") </script>';
                        }
                        else 
                        {
                            echo '<script type="text/javascript"> alert("Error!") </script>';
                        }
                    }
                }
            }
            ?>
        </div>
    </body>
</html>