View difference between Paste ID: GsCc89bW and eP5gXuNv
SHOW: | | - or go back to the newest paste.
1
<html>
2-
	<head>
2+
<head>
3-
		<title>My first PHP website</title>
3+
    <title>My first PHP website</title>
4-
	</head>
4+
</head>
5-
	<body>
5+
<body>
6-
		<h2>Registration Page</h2>
6+
<h2>Registration Page</h2>
7-
		<a href="index.php">Click here to go back</a><br/><br/>
7+
<a href="index.php">Click here to go back</a><br/><br/>
8-
		<form action="register.php" method="post">
8+
<form action="register.php" method="post">
9-
			Enter Username: <input type="text" name="username" required="required"/> <br/>
9+
    Enter Username: <input type="text" name="username" required="required"/> <br/>
10-
			Enter Password: <input type="password" name="password" required="required" /> <br/>
10+
    Enter Password: <input type="password" name="password" required="required" /> <br/>
11-
			<input type="submit" value="Register"/>
11+
    <input type="submit" value="Register"/>
12-
		</form>
12+
</form>
13-
	</body>
13+
</body>
14
</html>
15
16
<?php
17
if($_SERVER["REQUEST_METHOD"] == "POST"){
18-
	$username = mysql_real_escape_string($_POST['username']);
18+
    $username = mysqli_real_escape_string($_POST['username']);
19-
	$password = mysql_real_escape_string($_POST['password']);
19+
    $password = mysqli_real_escape_string($_POST['password']);
20
    $bool = true;
21-
	mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
21+
    $dbConnection = mysqli_connect("localhost", "root","") or die(mysqli_error()); //Connect to server
22-
	mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
22+
    mysqli_select_db($dbConnection, "first_db") or die("Cannot connect to database"); //Connect to database
23-
	$query = mysql_query("Select * from users"); //Query the users table
23+
    $query = mysqli_query($dbConnection, "Select * from users"); //Query the users table
24-
	while($row = mysql_fetch_array($query)) //Display all rows from query
24+
    while($row = mysqli_fetch_array($query)) //Display all rows from query
25-
	{
25+
    {
26-
		$table_users = $row['username']; //The first username row is passed on to $table_users, and so on until the query is finished
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
27+
        if($username == $table_users) //Checks if there are any matching fields
28-
		{
28+
        {
29-
			$bool = false; // sets bool to false
29+
            $bool = false; // sets bool to false
30-
			Print '<script>alert("Username has been taken!");</script>'; //Prompts the user
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
31+
            Print '<script>window.location.assign("register.php");</script>'; //Redirects to register.php
32-
		}
32+
        }
33-
	}
33+
    }
34-
	if($bool) // checks if bool is true
34+
    if($bool) // checks if bool is true
35-
	{
35+
    {
36-
		mysql_query("INSERT INTO users (username, password) VALUES ('$username','$password')"); //Inserts the value to table users
36+
        mysqli_query($dbConnection, "INSERT INTO users (username, password) VALUES ('$username','$password')"); //Inserts the value to table users
37-
		Print '<script>alert("Successfully Registered!");</script>'; //Prompts the user
37+
        Print '<script>alert("Successfully Registered!");</script>'; //Prompts the user
38-
		Print '<script>window.location.assign("register.php");</script>'; //Redirects to register.php
38+
        Print '<script>window.location.assign("register.php");</script>'; //Redirects to register.php
39-
	}
39+
    }
40
}
41
?>