Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>My first PHP website</title>
- </head>
- <body>
- <h2>Registration Page</h2>
- <a href="index.php">Click here to go back</a><br/><br/>
- <form action="register.php" method="post">
- Enter Username: <input type="text" name="username" required="required"/> <br/>
- Enter Password: <input type="password" name="password" required="required" /> <br/>
- <input type="submit" value="Register"/>
- </form>
- </body>
- </html>
- <?php
- if($_SERVER["REQUEST_METHOD"] == "POST"){
- $username = mysqli_real_escape_string($_POST['username']);
- $password = mysqli_real_escape_string($_POST['password']);
- $bool = true;
- $dbConnection = mysqli_connect("localhost", "root","") or die(mysqli_error()); //Connect to server
- mysqli_select_db($dbConnection, "first_db") or die("Cannot connect to database"); //Connect to database
- $query = mysqli_query($dbConnection, "Select COUNT(*) count from users WHERE username = '$username'");
- $result = mysqli_fetch_array($query);
- if(!empty($result['count'])) {
- $bool = false; // sets bool to false
- Print '<script>alert("Username has been taken!");</script>'; //Prompts the user
- Print '<script>window.location.assign("register.php");</script>'; //Redirects to register.php
- }
- if($bool) // checks if bool is true
- {
- mysqli_query($dbConnection, "INSERT INTO users (username, password) VALUES ('$username','$password')"); //Inserts the value to table users
- Print '<script>alert("Successfully Registered!");</script>'; //Prompts the user
- Print '<script>window.location.assign("register.php");</script>'; //Redirects to register.php
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement