Advertisement
Guest User

Untitled

a guest
Jun 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 = mysqli_real_escape_string($_POST['username']);
  19.     $password = mysqli_real_escape_string($_POST['password']);
  20.     $bool = true;
  21.     $dbConnection = mysqli_connect("localhost", "root","") or die(mysqli_error()); //Connect to server
  22.     mysqli_select_db($dbConnection, "first_db") or die("Cannot connect to database"); //Connect to database
  23.     $query = mysqli_query($dbConnection, "Select COUNT(*) count from users WHERE username = '$username'");
  24.     $result = mysqli_fetch_array($query);
  25.     if(!empty($result['count'])) {
  26.         $bool = false; // sets bool to false
  27.         Print '<script>alert("Username has been taken!");</script>'; //Prompts the user
  28.         Print '<script>window.location.assign("register.php");</script>'; //Redirects to register.php
  29.     }
  30.     if($bool) // checks if bool is true
  31.     {
  32.         mysqli_query($dbConnection, "INSERT INTO users (username, password) VALUES ('$username','$password')"); //Inserts the value to table users
  33.         Print '<script>alert("Successfully Registered!");</script>'; //Prompts the user
  34.         Print '<script>window.location.assign("register.php");</script>'; //Redirects to register.php
  35.     }
  36. }
  37. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement