在query()命令之前切断PHP。救命 :)

时间:2017-03-17 18:32:56

标签: php html mysql

我正在制作测试登录表单,而我正在使用localhosted mysql数据库。我试图通过使用名为pageid的id将整个登录表单放在一个文件中。每个pageid都有它自己的html页面,我拥有一切回声。但就在查询之前(...);命令,脚本停止在PHP中写入并将其作为普通文本放入。为什么这样,任何人都可以帮助解决它。如果有任何办法更聪明,我愿意接受建议。完整的代码在下面。谢谢你提前:))

<?php
    $connection = new mysqli("localhost", "root", "usbw", "loginform");

    if(!isset($_GET["pageid"])) {
        header("Location: index.php?pageid=1");
        die();
    } elseif($_GET["pageid"] == 1) {
        echo '
            <html>
                <head>
                    <style type="text/css">
                        html, body {
                            height: 100%;
                        }

                        html {
                            display: table;
                            margin: auto;
                        }

                        body {
                            background-color: rgb(208, 128, 0);
                            display: table-cell;
                            vertical-align: middle;
                        }

                        #username, #password {
                            width: 300px;
                            height: 25px;
                            font-size: 25px;
                            border-color: black;
                        }

                        #login, #register {
                            font-size: 25px;
                            background: transparent;
                            border-color: black;
                        }

                        #register {
                            margin-left: 114px;
                        }
                    </style>
                </head>
                <body>
                    <form method="post" action="index.php?pageid=7">
                        <input type="text" id="username" placeholder="Username"><br><br>
                        <input type="password" id="password" placeholder="Password"><br><br>
                        <input type="submit" id="login" value="Login">
                        <input type="button" id="register" value="Register" onclick="window.location = \'index.php?pageid=2\';">
                    </form>

                    <?php
                        if(isset($_POST["username"]) & isset($_POST["password"])) {
                            $usernameHash = hash("sha512", $_POST["username"]);
                            $passwordHash = hash("sha512", $_POST["password"]);

                            $request = "SELECT * FROM users";
                            $result = $connection -> query($request);
                            while($user = $result -> fetch_assoc()) {
                                if($user["username"] == $usernameHash) {
                                    if($user["password"] == $passwordHash) {
                                        header("Location: index.php?pageid=3&userid=" . $user["id"]);
                                        die();
                                    } else {
                                        header("Location: index.php?pageid=4");
                                        die();
                                    }
                                } else {
                                    header("Location: index.php?pageid=4");
                                    die();
                                }
                            }
                        } else {
                            echo "not ok";
                        }
                    ?>
                </body>
            </html>
        ';
    } elseif($_GET["pageid"] == 2) {
        echo '
            <html>
                <head>
                    <style type="text/css">
                        html, body {
                            height: 100%;
                        }

                        html {
                            display: table;
                            margin: auto;
                        }

                        body {
                            background-color: rgb(208, 128, 0);
                            display: table-cell;
                            vertical-align: middle;
                        }

                        #usernameReg, #passwordReg, #passwordConfirm, #firstName, #lastName, #email, #phoneNr {
                            width: 300px;
                            height: 25px;
                            font-size: 25px;
                            border-color: black;
                        }

                        #firstName, #lastName {
                            width: 233px;
                        }

                        #email {
                            width: 400px;
                        }

                        #phoneNr {
                            width: 200px;
                        }

                        #register {
                            font-size: 25px;
                            background: transparent;
                            border-color: black;
                        }
                    </style>
                </head>
                <body>
                    <form method="post" action="index.php?pageid=8">
                        <input type="text" id="usernameReg" placeholder="New username"><br><br>
                        <input type="password" id="passwordReg" placeholder="New password"><br><br>
                        <input type="password" id="passwordConfirm" placeholder="Confirm password"><br><br><br>
                        <input type="text" id="firstName" placeholder="First name"><br><br>
                        <input type="text" id="lastName" placeholder="Last name"><br><br><br>
                        <input type="text" id="email" placeholder="New email"><br><br>
                        <input type="text" id="phoneNr" placeholder="Phone number"><br><br>
                        <input type="submit" id="register" value="Register">
                    </form>

                    <?php
                        if(isset($_POST["usernameReg"]) & isset($_POST["passwordReg"]) & isset($_POST["passwordConfirm"]) & isset($_POST["firstName"]) & isset($_POST["lastName"]) & isset($_POST["email"]) & isset($_POST["phoneNr"])) {
                            if($_POST["passwordReg"] != $_POST["passwordConfirm"]) {
                                header("Location: index.php?pageid=6");
                                die();
                            }

                            $usernameHash = hash("sha512", $_POST["usernameReg"]);
                            $passwordHash = hash("sha512", $_POST["passwordReg"]);
                            $firstNameHash = hash("sha512", $_POST["firstName"]);
                            $lastNameHash = hash("sha512", $_POST["lastName"]);
                            $emailHash = hash("sha512", $_POST["email"]);
                            $phoneNrHash = hash("sha512", $_POST["phoneNr"]);

                            $request = "SELECT * FROM users";
                            $result = $connection -> query($request);
                            while($user = $result -> fetch_assoc()) {
                                if($user["username"] != $usernameHash) {
                                    if($user["email"] != $emailHash) {
                                        if($user["phoneNr"] != $phoneNrHash) {
                                            $request = "INSERT INTO users (`username`, `password`, `firstName`, `lastName`, `email`, `phoneNr`) VALUES (\'" . $usernameHash . "\', \'" . $passwordHash . "\', \'" . $firstNameHash . "\', \'" . $lastNameHash . "\', \'" . $emailHash . "\', \'" . $phoneNrHash . "\')";
                                            $result = $connection -> query($request);

                                            header("Location: index.php?pageid=5");
                                            die();
                                        } else {
                                            header("Location: index.php?pageid=6");
                                            die();
                                        }
                                    } else {
                                        header("Location: index.php?pageid=6");
                                        die();
                                    }
                                } else {
                                    header("Location: index.php?pageid=6");
                                    die();
                                } 
                            }
                        } else {
                            echo "not ok";
                        }
                    ?>
                </body>
            </html>
        ';
    } elseif($_GET["pageid"] == 3) {

    } elseif($_GET["pageid"] == 4) {

    } elseif($_GET["pageid"] == 5) {

    } elseif($_GET["pageid"] == 6) {

    }
?>

This is how it looks, but shouldn't look. (i.stack.imgur link)

1 个答案:

答案 0 :(得分:-1)

虽然这不是打印html页面的正确方法,但替换为以下内容以使其正常工作

<?php
    $connection = new mysqli("localhost", "root", "usbw", "loginform");

    if(!isset($_GET["pageid"])) {
        header("Location: index.php?pageid=1");
        die();
    } elseif($_GET["pageid"] == 1) {
        echo '
            <html>
                <head>
                    <style type="text/css">
                        html, body {
                            height: 100%;
                        }

                        html {
                            display: table;
                            margin: auto;
                        }

                        body {
                            background-color: rgb(208, 128, 0);
                            display: table-cell;
                            vertical-align: middle;
                        }

                        #username, #password {
                            width: 300px;
                            height: 25px;
                            font-size: 25px;
                            border-color: black;
                        }

                        #login, #register {
                            font-size: 25px;
                            background: transparent;
                            border-color: black;
                        }

                        #register {
                            margin-left: 114px;
                        }
                    </style>
                </head>
                <body>
                    <form method="post" action="index.php?pageid=7">
                        <input type="text" id="username" placeholder="Username"><br><br>
                        <input type="password" id="password" placeholder="Password"><br><br>
                        <input type="submit" id="login" value="Login">
                        <input type="button" id="register" value="Register" onclick="window.location = \'index.php?pageid=2\';">
                    </form>

                    <?php
                        if(isset($_POST["username"]) & isset($_POST["password"])) {
                            $usernameHash = hash("sha512", $_POST["username"]);
                            $passwordHash = hash("sha512", $_POST["password"]);

                            $request = "SELECT * FROM users";
                            $result = $connection -> query($request);
                            while($user = $result -> fetch_assoc()) {
                                if($user["username"] == $usernameHash) {
                                    if($user["password"] == $passwordHash) {
                                        header("Location: index.php?pageid=3&userid=" . $user["id"]);
                                        die();
                                    } else {
                                        header("Location: index.php?pageid=4");
                                        die();
                                    }
                                } else {
                                    header("Location: index.php?pageid=4");
                                    die();
                                }
                            }
                        } else {
                            echo "not ok";
                        }
                    ?>
                </body>
            </html>
        ';
    } elseif($_GET["pageid"] == 2) {
        echo '
            <html>
                <head>
                    <style type="text/css">
                        html, body {
                            height: 100%;
                        }

                        html {
                            display: table;
                            margin: auto;
                        }

                        body {
                            background-color: rgb(208, 128, 0);
                            display: table-cell;
                            vertical-align: middle;
                        }

                        #usernameReg, #passwordReg, #passwordConfirm, #firstName, #lastName, #email, #phoneNr {
                            width: 300px;
                            height: 25px;
                            font-size: 25px;
                            border-color: black;
                        }

                        #firstName, #lastName {
                            width: 233px;
                        }

                        #email {
                            width: 400px;
                        }

                        #phoneNr {
                            width: 200px;
                        }

                        #register {
                            font-size: 25px;
                            background: transparent;
                            border-color: black;
                        }
                    </style>
                </head>
                <body>
                    <form method="post" action="index.php?pageid=8">
                        <input type="text" id="usernameReg" placeholder="New username"><br><br>
                        <input type="password" id="passwordReg" placeholder="New password"><br><br>
                        <input type="password" id="passwordConfirm" placeholder="Confirm password"><br><br><br>
                        <input type="text" id="firstName" placeholder="First name"><br><br>
                        <input type="text" id="lastName" placeholder="Last name"><br><br><br>
                        <input type="text" id="email" placeholder="New email"><br><br>
                        <input type="text" id="phoneNr" placeholder="Phone number"><br><br>
                        <input type="submit" id="register" value="Register">
                    </form>


                </body>
            </html>
        ';

                        if(isset($_POST["usernameReg"]) & isset($_POST["passwordReg"]) & isset($_POST["passwordConfirm"]) & isset($_POST["firstName"]) & isset($_POST["lastName"]) & isset($_POST["email"]) & isset($_POST["phoneNr"])) {
                            if($_POST["passwordReg"] != $_POST["passwordConfirm"]) {
                                header("Location: index.php?pageid=6");
                                die();
                            }

                            $usernameHash = hash("sha512", $_POST["usernameReg"]);
                            $passwordHash = hash("sha512", $_POST["passwordReg"]);
                            $firstNameHash = hash("sha512", $_POST["firstName"]);
                            $lastNameHash = hash("sha512", $_POST["lastName"]);
                            $emailHash = hash("sha512", $_POST["email"]);
                            $phoneNrHash = hash("sha512", $_POST["phoneNr"]);

                            $request = "SELECT * FROM users";
                            $result = $connection -> query($request);
                            while($user = $result -> fetch_assoc()) {
                                if($user["username"] != $usernameHash) {
                                    if($user["email"] != $emailHash) {
                                        if($user["phoneNr"] != $phoneNrHash) {
                                            $request = "INSERT INTO users (`username`, `password`, `firstName`, `lastName`, `email`, `phoneNr`) VALUES (\'" . $usernameHash . "\', \'" . $passwordHash . "\', \'" . $firstNameHash . "\', \'" . $lastNameHash . "\', \'" . $emailHash . "\', \'" . $phoneNrHash . "\')";
                                            $result = $connection -> query($request);

                                            header("Location: index.php?pageid=5");
                                            die();
                                        } else {
                                            header("Location: index.php?pageid=6");
                                            die();
                                        }
                                    } else {
                                        header("Location: index.php?pageid=6");
                                        die();
                                    }
                                } else {
                                    header("Location: index.php?pageid=6");
                                    die();
                                } 
                            }
                        } else {
                            echo "not ok";
                        }

    } elseif($_GET["pageid"] == 3) {

    } elseif($_GET["pageid"] == 4) {

    } elseif($_GET["pageid"] == 5) {

    } elseif($_GET["pageid"] == 6) {

    }
?>