Advertisement
Guest User

h

a guest
Sep 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.73 KB | None | 0 0
  1. <html>
  2.     <head>
  3.         <title>My first PHP website</title>
  4.     </head>
  5.     <body>
  6.         <h2>Registration Page</h2>
  7.         <a href="index.php">Click here to go back</a><br/><br/>
  8.         <form action="register.php" method="post">
  9.             Enter Username: <input type="text" name="username" required="required"/> <br/>
  10.             Enter Password: <input type="password" name="password" required="required" /> <br/>
  11.             <input type="submit" value="Register"/>
  12.         </form>
  13.     </body>
  14. </html>
  15.  
  16. <?php
  17. if($_SERVER["REQUEST_METHOD"] == "POST"){
  18.     $username = mysql_real_escape_string($_POST['username']);
  19.     $password = mysql_real_escape_string($_POST['password']);
  20.     $bool = true;
  21.     mysql_connect("localhost", "root","1234") or die(mysql_error()); //Connect to server
  22.     mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
  23.     $query = mysql_query("Select * from users"); //Query the users table
  24.     while($row = mysql_fetch_array($query)) //display all rows from query
  25.     {
  26.         $table_users = $row['username']; // the first username row is passed on to $table_users, and so on until the query is finished
  27.         if($username == $table_users) // checks if there are any matching fields
  28.         {
  29.             $bool = false; // sets bool to false
  30.             Print '<script>alert("Username has been taken!");</script>'; //Prompts the user
  31.             Print '<script>window.location.assign("register.php");</script>'; // redirects to register.php
  32.         }
  33.     }
  34.     if($bool) // checks if bool is true
  35.     {
  36.         mysql_query("INSERT INTO users (username, password) VALUES ('$username','$password')"); //Inserts the value to table users
  37.         Print '<script>alert("Successfully Registered!");</script>'; // Prompts the user
  38.         Print '<script>window.location.assign("register.php");</script>'; // redirects to register.php
  39.     }
  40. }
  41. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement