Sign Up Process

Create Login with PHP and MySQL
Create Login with PHP and MySQL : Home
Algorithm
Sign Up Process
Log In Process
Log Out Process

Sign Up
We have created a table users[email VARCHAR(250) PRIMARY KEY, name VARCHAR(250), password VARCHAR(250)] in db.

Now we will create a html form for Sign Up. The form should have 3 Input fields for email, name, password. We have created a file sign-up-form.php and also another file sign-up-action.php where we will handle the user's input and perform queries in database.
The user should be redirected to this file once he submits the form. So action="" attribute of form should have value like this action="sign-up-action.php"

sign-up-form.php

<html>
<html>
<body>
<form method="post" action="sign-up-action.php">
	<input type="text" name="signup-email" placeholder="email" required="required">
	<input type="text" name="signup-name" placeholder="name" required="required">
	<input type="password" name="signup-password" placeholder="password" required="required">
	<input type="submit" name="submit">
</form>

</body>
</html>

Once the user submits form, the browser will be directed to sign-up-action.php, so here we will perform the queries and put user in the database.

<?php
// Checkinng if we have data from signup form

if(isset($_POST['signup-email'])&&isset($_POST['signup-name'])&&isset($_POST['signup-password'])){
	
	// Converting any special characters to entities and email to lowercase
	// So that same email is not considered different

	$email = htmlspecialchars(strtolower($_POST['signup-email']));
	$name = htmlspecialchars($_POST['signup-name']);
	// Encrypting Password
	$password = md5($_POST['signup-password']);

	// Checking if user already exists for that email

	$mysqli_object = new mysqli("localhost","","","db");
	$sql_check ="SELECT * FROM users WHERE email='".$email."'";
	$result_check = $mysqli_object->query($sql_check);
	if($result_check->num_rows>0){
		echo "User already exists";
	}
	// If email is not already in database i.e. new user

	else{
		// Adding user in database
		$sql_add = "INSERT INTO users(email,name,password) VALUES('".$email."','".$name."','".$password."')";
		if($mysqli_object->query($sql_add)){
			echo $email." successfully signed Up";

		}

	}

}
?>

If you are having problem understanding how we embedded PHP variables in SQL queries i.e. Line No 17 & 26, just understand that SQL queries is just a string.
For example in line 17, we have email abc@xyz.com. So the query would be like "SELECT * FROM users WHERE email='abc@xyz.com'"
Now as we know that simply using PHP variable inside literal string can cause trouble. So consider string before email as one string ("SELECT * FROM users WHERE email=") and after email as another string ("'"). Now we are just concatenating a variable between two strings i.e. before_email.$variable.after_email. Do this and you will come up with the same code we used in Line 17 & 26.
You can also understand this as the quote before variable is to end the literal string . is to concatenate and quote after is to start literal string again.
Combing both files
Instead of having these two files we could have cobined both files as one file. This can be done using

Lets see how it works out. We are creating one file sign-up.php and form will have action="" blank so that it redirect to itself after submitting the form. We will simply add an else at the end of sign-up-action.php and put HTML form in it

sign-up.php

<?php
// Checkinng if we have data from signup form

if(isset($_POST['signup-email'])&&isset($_POST['signup-name'])&&isset($_POST['signup-password'])){
	
	// Converting any special characters to entities and email to lowercase
	// So that same email is not considered different

	$email = htmlspecialchars(strtolower($_POST['signup-email']));
	$name = htmlspecialchars($_POST['signup-name']);
	// Encrypting Password
	$password = md5($_POST['signup-password']);

	// Checking if user already exists for that email

	$mysqli_object = new mysqli("localhost","","","db");
	$sql_check ="SELECT * FROM users WHERE email='".$email."'";
	$result_check = $mysqli_object->query($sql_check);
	if($result_check->num_rows>0){
		echo "User already exists";
	}
	// If email is not already in database i.e. new user

	else{
		// Adding user in database
		$sql_add = "INSERT INTO users(email,name,password) VALUES('".$email."','".$name."','".$password."')";
		if($mysqli_object->query($sql_add)){
			echo $email." successfully signed Up";

		}

	}

}
// If POST data is not available i.e. form is not submitted
else{
	// Embedding HTML in PHP
	?>
<html>
<body>
<form method="post" action="sign-up.php">
	<input type="text" name="signup-email" placeholder="email" required="required">
	<input type="text" name="signup-name" placeholder="name" required="required">
	<input type="password" name="signup-password" placeholder="password" required="required">
	<input type="submit" name="submit">
</form>

</body>
</html>
	<?php

}
?>

Login to Track Your Progress