View difference between Paste ID: rTvDcHpJ and GsCc89bW
SHOW: | | - or go back to the newest paste.
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 * from users"); //Query the users table
23+
    $query = mysqli_query($dbConnection, "Select COUNT(*) count from users WHERE username = '$username'");
24-
    while($row = mysqli_fetch_array($query)) //Display all rows from query
24+
    $result = mysqli_fetch_array($query);
25
    if(!empty($result['count'])) {
26-
        $table_users = $row['username']; //The first username row is passed on to $table_users, and so on until the query is finished
26+
        $bool = false; // sets bool to false
27-
        if($username == $table_users) //Checks if there are any matching fields
27+
        Print '<script>alert("Username has been taken!");</script>'; //Prompts the user
28-
        {
28+
29-
            $bool = false; // sets bool to false
29+
30-
            Print '<script>alert("Username has been taken!");</script>'; //Prompts the user
30+
31-
            Print '<script>window.location.assign("register.php");</script>'; //Redirects to register.php
31+
32-
        }
32+
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
?>