2017-03-17 92 views
-2

我正在做一个测试loginform,我使用的是localhosted mysql数据库。我试图通过使用一个名为pageid的ID在一个文件中拥有整个登录表单。每个pageid都有它自己的html页面,并且我拥有一切回应。但在查询之前(...);命令,脚本将停止使用PHP编写并将其作为普通文本进行编写。为什么是这样的,任何人都可以帮助解决问题。如果有什么方法可以做到这一点,我很乐意提供建议。底下有完整的代码。对提前:)PHP在query()命令之前被截断。帮助:)

<?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)

+3

你把你的'<更换正确的方式? php''在'echo'语句中打开标签。 – miken32

+1

这不是你想要做的事情。尽可能将代码与HTML分开。你应该最终得到一个偶尔有''的HTML文件。除了循环和条件之外,没有任何逻辑。 – miken32

回答

-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) { 

    } 
?>