codescracker


php

PHP and MySQLi SignUp Page



« Previous Tutorial Next Tutorial »


Here you will learn about how to create signup page or registration form using PHP and MySQLi with example code.

What to do Before Creating SignUp Page

Before creating signup page or user registration form using PHP and MySQLi, make sure that you have already learned and created a table named users in database named codescracker as shown in the last part of create table tutorial using PHP and MySQLi.

PHP MySQLi Simple SignUp Page Example

Here is a simple example of signup page or user registration form using PHP and MySQLi.

Let's first create signup form web page using HTML only.

<html>
<head>
	<title>PHP and MySQLi User Registration or SignUp Page Example</title>
</head>
<body>

<h2>Create Account or SignUp Page</h2>
<form action="codescrackerSignupFeed.php" method="post">
Username:<br/>
<input type="text" name="username" maxlength="40" placeholder="create username" required><br/>
Password:<br/>
<input type="password" name="password" placeholder="create password"  maxlength="40" required><br/>
Email ID:<br/>
<input type="email" name="emailid" placeholder="enter email id"  maxlength="40" required><br/>
Name:<br/>
<input type="text" name="name" placeholder="enter full name"  maxlength="40" required><br/>
Sex:<br/>
<select name="sex" required>
<option value="">Select Sex</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select><br/>
DOB:<br/>
<input type="date" name="dob" value="choose DOB" required><br/><br/>
<input type="submit" name="signup" value="SignUp">
</form>

</body>
</html>

Type the above code or just do copy and paste the above code and save it inside the directory C:\xampp\htdocs\ with name codescrackerSignup.php.

Here is the output produce by the above example code of signup page.

php mysqli signup page

Here is the PHP and MySQLi code to process the above form.

<?php 
	// below code checks whether the form is submitted
	// using the POST method or not
	if($_SERVER["REQUEST_METHOD"] == "POST")
	{
		// the form is submitted using the POST method
		// now proceed to process the form's data
		$server = "localhost";
		$user = "root";
		$pass = "";
		$database = "codescracker";
		$conn = mysqli_connect($server, $user, $pass, $database);
		if(mysqli_connect_error())
		{
			echo "<p>Error occurred..kindly try again later.</p>";
			echo "<p>Exiting...</p>";
			exit();
		}
		$errUser = $errPass = $errEmail = $errName = $errSex = $errDOB = "";
		$user = $pass = $email = $name = $sex = $dob = "";
		$user = $_POST["username"];
		$pass = $_POST["password"];
		$email = $_POST["emailid"];
		$name = $_POST["name"];
		$sex = $_POST["sex"];
		$dob = $_POST["dob"];
	}
?>
<html>
<head>
	<title>PHP and MySQLi Registering the User</title>
</head>
<body>
<?php 
	if($_SERVER["REQUEST_METHOD"] == "POST")
	{
		$qry = "insert into users(username, password, emailid, fullname, sex, dob)
				values('$user', '$pass', '$email', '$name', '$sex', '$dob');";
		$res = $conn->query($qry);
		if($res)
		{
			echo "<p>You are successfully registered to codescracker.com</p>";
			echo "<p>Your Username: <b>".$user."</b></p>";
			echo "<p>Your Password: <b>".$pass."</b></p>";
			exit();
		}
		else 
		{
			echo "<p>Error occurred, kindly try again later.</p>";
			exit();
		}
	}
?>
</body>
</html>

Save the above file with name codescrackerSignupFeed.php inside the same directory.

Now fill all the data in the form as shown below. This is just a demo of feeded data, you can feed your own.

php mysqli user registration form

After entering all the data, click on the SignUp button, and you will be redirected to the codescrackerSignupFeed.php page which will process the form data and insert all the data into the table.

Here is the sample output that will produce after filling the data and clicking on the SignUp button.

php mysqli signup page example

And below is the screenshot of the table users after performing the above example code of signup page and registering user into the database.

create signup page using php mysqli

PHP MySQLi Create and Process SignUp Page on Same Page

Here is an example shows how to create and process signup page on the same page using PHP and MySQLi.

<?php 
	if($_SERVER["REQUEST_METHOD"] == "POST")
	{
		$conn = mysqli_connect('localhost', 'root', '', 'codescracker');
		if(mysqli_connect_error())
		{
			echo "<p>Error occurred..kindly try again later.</p>";
			exit();
		}
		$errUser = $errPass = $errEmail = $errName = $errSex = $errDOB = "";
		$user = $pass = $email = $name = $sex = $dob = "";
		$user = $_POST["username"];
		$pass = $_POST["password"];
		$email = $_POST["emailid"];
		$name = $_POST["name"];
		$sex = $_POST["sex"];
		$dob = $_POST["dob"];
	}
?>
<html>
<head>
	<title>PHP and MySQLi User Registration or SignUp Page Example</title>
</head>
<body>

<?php 
	if($_SERVER["REQUEST_METHOD"] == "POST")
	{
		$qry = "insert into users(username, password, emailid, fullname, sex, dob)
				values('$user', '$pass', '$email', '$name', '$sex', '$dob');";
		$res = $conn->query($qry);
		if($res)
		{
			echo "<p>You are successfully registered to codescracker.com</p>";
			echo "<p>Your Username: <b>".$user."</b></p>";
			echo "<p>Your Password: <b>".$pass."</b></p>";
			echo "<p>Your Email ID: <b>".$email."</b></p>";
			exit();
		}
		else 
		{
			echo "<p>Error occurred, kindly try again later.</p>";
			exit();
		}
	}
?>

<h2>Create Account or SignUp Page</h2>
<form method="post">
Username:<br/>
<input type="text" name="username" maxlength="40" placeholder="create username" required><br/>
Password:<br/>
<input type="password" name="password" placeholder="create password"  maxlength="40" required><br/>
Email ID:<br/>
<input type="email" name="emailid" placeholder="enter email id"  maxlength="40" required><br/>
Name:<br/>
<input type="text" name="name" placeholder="enter full name"  maxlength="40" required><br/>
Sex:<br/>
<select name="sex" required>
<option value="">Select Sex</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select><br/>
DOB:<br/>
<input type="date" name="dob" value="choose DOB" required><br/><br/>
<input type="submit" name="signup" value="SignUp">
</form>

</body>
</html>

Here is the sample output produced by the above example code of signup page using PHP and MySQLi.

The output given below is filled with some sample data.

php mysqli create signup page example

Now after filling the details, click on the SignUp button, and here is the sample output you will see in your browser.

php mysqli registration page example

PHP MySQLi SignUp Page with Validation

Here is an example of creating signup page using PHP and MySQLi with validation of the user's data.

To include validation code in the above example to validate the user's data then insert the data into the database for security purpose. Then replace the following code:

		$user = $_POST["username"];
		$pass = $_POST["password"];
		$email = $_POST["emailid"];
		$name = $_POST["name"];
		$sex = $_POST["sex"];
		$dob = $_POST["dob"];
	}

with

		$user = $_POST["username"];
		$user = validate_filter_signup($user);
		$pass = $_POST["password"];
		$pass = validate_filter_signup($pass);
		$email = $_POST["emailid"];
		$email = validate_filter_signup($email);
		$name = $_POST["name"];
		$name = validate_filter_signup($name);
		$sex = $_POST["sex"];
		$dob = $_POST["dob"];
	}
	function validate_filter_signup($signupData)
	{
		$signupData = trim($signupData);
		$signupData = stripslashes($signupData);
		$signupData = htmlspecialchars($signupData);
		return $signupData;
	}

PHP MySQLi Complete SignUp Page Example Code

Here is the complete version of the signup page example code using PHP and MySQLi.

<?php 
	if($_SERVER["REQUEST_METHOD"] == "POST")
	{
		$server = "localhost";
		$user = "root";
		$pass = "";
		$database = "codescracker";
		$conn = mysqli_connect($server, $user, $pass, $database);
		if(mysqli_connect_error())
		{
			echo "<p>Error occurred..kindly try again later.</p>";
			echo "<p>Exiting...</p>";
			exit();
		}
		$errUser = $errPass = $errEmail = $errName = $errSex = $errDOB = "";
		$user = $pass = $email = $name = $sex = $dob = "";
		if(empty($_POST["username"]))
		{
			$errUser = "You forgot create your username";
		}
		else 
		{
			$user = $_POST["username"];
			$user = validate_filter_signup($user);
		}
		if(empty($_POST["password"]))
		{
			$errPass = "You forgot to create your password";
		}
		else 
		{
			$pass = $_POST["password"];
			$pass = validate_filter_signup($pass);
		}
		if(empty($_POST["emailid"]))
		{
			$errEmail = "You forgot to enter your email id";
		}
		else 
		{
			$email = $_POST["emailid"];
			$email = validate_filter_signup($email);
		}
		if(empty($_POST["name"]))
		{
			$errName = "You forgot to enter your name";
		}
		else 
		{
			$name = $_POST["name"];
			$name = validate_filter_signup($name);
		}
		if(empty($_POST["sex"]))
		{
			$errSex = "You forgot to choose your sex";
		}
		else 
		{
			$sex = $_POST["sex"];
		}
		if(empty($_POST["dob"]))
		{
			$errDOB = "You forgot to select your date of birth";
		}
		else 
		{
			$dob = $_POST["dob"];
		}
	}
	function validate_filter_signup($signupData)
	{
		$signupData = trim($signupData);
		$signupData = stripslashes($signupData);
		$signupData = htmlspecialchars($signupData);
		return $signupData;
	}
?>
<html>
<head>
	<title>PHP and MySQLi Complete SignUp Page Example Code</title>
	<script>
	function checkBeforeSubmit()
	{
		if(document.signupForm.username.value=="")
		{
			alert("Create your username.");
			document.signupForm.username.focus();
			return false;
		}
		if(document.signupForm.password.value=="")
		{
			alert("Create your password.");
			document.signupForm.password.focus();
			return false;
		} 
		if(document.signupForm.emailid.value=="")
		{
			alert("Enter your email id.");
			document.signupForm.emailid.focus();
			return false;
		} 
		if(document.signupForm.name.value=="")
		{
			alert("Enter your name.");
			document.signupForm.name.focus();
			return false;
		}
		if(document.signupForm.sex.value=="")
		{
			alert("Select your sex.");
			document.signupForm.sex.focus();
			return false;
		}
		if(document.signupForm.dob.value=="")
		{
			alert("Select your dob.");
			document.signupForm.dob.focus();
			return false;
		}
		return true;
	}
	</script>
</head>
<body>
<?php 
if($_SERVER["REQUEST_METHOD"] == "POST")
{
	if(!empty($errUser) || !empty($errPass) || !empty($errEmail) || !empty($errName) || !empty($errSex) || !empty($errDOB))
	{
		if(!empty($errUser))
		{
			echo "<b>Username Error Message: </b>";
			echo $errUser;
			echo "<br/>";
		}
		if(!empty($errPass))
		{
			echo "<b>Passowrd Error Message: </b>";
			echo $errPass;
			echo "<br/>";
		}
		if(!empty($errEmail))
		{
			echo "<b>Email ID Error Message: </b>";
			echo $errEmail;
			echo "<br/>";
		}
		if(!empty($errName))
		{
			echo "<b>Name Error Message: </b>";
			echo $errName;
			echo "<br/>";
		}
		if(!empty($errSex))
		{
			echo "<b>Sex Error Message: </b>";
			echo $errSex;
			echo "<br/>";
		}
		if(!empty($errDOB))
		{
			echo "<b>DOB Error Message: </b>";
			echo $errDOB;
			echo "<br/>";
		}
		echo "<p>Kindly try again to <a href=\"codescracker.php\">SignUp</a> on codescracker</p>";
	}
	else 
	{
		$qry = "select * from users where username='$user'";
		$rsl = $conn->query($qry);
		if(mysqli_num_rows($rsl)>0)
		{
			echo "<p>Username already existed in the database.</p>";
			echo "<p>Kindly try with different one.</p>";
			echo "<p>Go back to <a href=\"codescracker.php\">SignUp</a> Page</a></p>";
			exit();
		}
		else 
		{
			$qry = "insert into users(username, password, emailid, fullname, sex, dob)
					values('$user', '$pass', '$email', '$name', '$sex', '$dob');";
			$res = $conn->query($qry);
			if($res)
			{
				echo "<p>You are successfully registered to codescracker.com</p>";
				echo "<p>Your Username: <b>".$user."</b></p>";
				echo "<p>Your Password: <b>".$pass."</b></p>";
				exit();
			}
		}
	}
}
?>

<h2>Create Account or SignUp Page</h2>
<form name="signupForm" method="post" onsubmit="return checkBeforeSubmit()">
Username:<br/>
<input type="text" name="username" maxlength="40" placeholder="create username" required><br/>
Password:<br/>
<input type="password" name="password" placeholder="create password"  maxlength="40" required><br/>
Email ID:<br/>
<input type="email" name="emailid" placeholder="enter email id"  maxlength="40" required><br/>
Name:<br/>
<input type="text" name="name" placeholder="enter full name"  maxlength="40" required><br/>
Sex:<br/>
<select name="sex" required>
<option value="">Select Sex</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select><br/>
DOB:<br/>
<input type="date" name="dob" value="choose DOB" required><br/><br/>
<input type="submit" name="signup" value="SignUp">
</form>

</body>
</html>

If you run the above example code of complete signup page using PHP and MySQLi and click on the SignUp button without entering any detail, then here is the sample output that will be shown in your browser:

php mysqli complete signup page example

This alert message is shown because we have inserted the JavaScript code into the above example just to fetch all the input box before submit the form to check whether all the input field is filled or not.

Now enter the username as codescracker and then enter all the detail as you want, but enter the username as codescracker. This username is already created using the above simple example of signup page using PHP and MySQLi.

Now after entering the already existed username along with some other details, then here is the output you will see in your web browser when pressing the SignUp button.

php mysqli complete signup page code

« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test