In previous post we have learned to create a secure login system with session. Now let’s have look at How to Create a Registration Form with PHP and MySQLi. Well it’s quite easy and can be use for different purposes. It can be use to Register a new member on your membership website or you can use it to get some inputs from the user for further processing, well it doesn’t matter for what you want a registration form the code is same for all just need a little bit knowledge of PHP and MySQLi you can create your own one. Here I am going to explain each and everything for registration form, but still you can start a discussion below if you need any type of help or you are confuse at any point.


So, first we need a HTML form that include some input fields and a submit button.

<form method="post" action="">
<legend>Registration Form</legend>
<table width="400" border="0" cellpadding="10" cellspacing="10">
<td style="font-weight: bold"><div align="right"><label for="name">Name</label></div></td>
<td><input name="name" type="text" class="input" size="25" required /></td>
<td style="font-weight: bold"><div align="right"><label for="email">Email</label></div></td>
<td><input name="email" type="email" class="input" size="25" required /></td>
<td height="23" style="font-weight: bold"><div align="right"><label for="password">Password</label></div></td>
<td><input name="password" type="password" class="input" size="25" required /></td>
<td height="23"></td>
<td><div align="right">
  <input type="submit" name="submit" value="Register!" />

Then move to PHP code. As we are working with PHP and MySQLi. So, definitely we need to connect our page with database. Here is the database structure:

CREATE TABLE `users` (
  `uid` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`uid`),
  UNIQUE KEY `username` (`email`)

Now, connect your PHP Code with MySQL Database.


define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database_name');


Now, we need to perform registration process when user hit ‘Register’ button. For this purpose we will use PHP isset() function as:

	//Code goes here.

Under this if statement we will perform all our actions. First we will use mysqli_real_escape_string() function escape special character in a string to for use in an SQL Statement. It also help us to prevent from SQL Injection Attack.

$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];

$name = mysqli_real_escape_string($db, $name);
$email = mysqli_real_escape_string($db, $email);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);

md5() is a PHP fucntion, used for password protection. It encrypt data entered by the user and store in special format that can’t be decrypted. You can read more about md5() from here.

OK, it was basic structure for our PHP Code. Now lets move towards actual code. When user entered his email, we need to check it this email already exist or not. If it exist then it will print error “Sorry…This email already exist…“. Here is how we can check email.

$sql = "SELECT email FROM users WHERE email='$email'";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);

if(mysqli_num_rows($result) == 1)
	echo "Sorry...This email already exist..";
	//Code goes here.

What does this code will do? First it will perform MySQLi query to check all rows in database. If it found entered email in database it will print out error of already existed email otherwise it will insert user record in database by using the following code.

$query = mysqli_query($db, "INSERT INTO users (name, email, password)VALUES ('$name', '$email', '$password')");

	echo "Thank You! you are now registered.";

That’s all! Now you have your own Registration Form. You can use it for different purposes depends on your requirement.



  1. Hi,
    I have followed and integrated this script in my project and it works great.
    But there is one thing that is bothering me,

    I see that the fields have validation on them, and they work great. BUT I don’t see any javascript or PHP validation code anywhere in the tutorial or in the files. SO how is that validation working?

  2. Hi, thanks for your reply man, but if you go to the demo and just press register, a windows pops out right under the textbox for username saying “Please fill out this feild” and so on for the rest of the feilds.

    That is what I am asking, or am i the only one getting that 😛

  3. I havent edited the code but it shows the followin error.

    Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /home/u211451211/public_html/index.php on line 12

    Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /home/u211451211/public_html/index.php on line 13

    Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /home/u211451211/public_html/index.php on line 14

    Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/u211451211/public_html/index.php on line 19

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/u211451211/public_html/index.php on line 20

    Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/u211451211/public_html/index.php on line 21

    Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/u211451211/public_html/index.php on line 27

  4. hi sir, i have question, how im going to combine the login and the reg form in one table using your code? hope you could help me. thanks and more power

  5. Hi sir, I am facing one issue in this form. after clicking on register, the previous values are not getting cleared from the form. I tried unset($_POST); and also $_POST = array();

    None of the ways worked fine. Please guide. I am very new to php.

  6. Usually everything got clear after submitting the form in PHP. But I don’t know maybe you have edit my given code. Can you please email me your code so that I will check your code and let you know as I get free from work 🙂


  7. Hello,
    I’m completely new to php and SQL.
    I’ve used a different registration form before and i couldn’t get it to work until I noticed that with php7 there are some big changes regarding the mysql_function. So I had to change the server to php5.6 for now, though I’d really like to use php7. Since you’re using mysqli, does your form work with php7?

  8. Why did you not use the sha512 Hash, instead of the md5 hash? If I was you I would change

    $password = md5($password);


    $salt = “PleaseDontUseARainbowTable12345”;
    $data = $password . $salt;
    $password = hash(‘sha512’, $data);

    I tested it and I will work 🙂

  9. Hi, for me when I start programming all I know is CSS, HTML and JavaScript when I later know what PHP does I do write it in a diary but I am not good yet well I believe I will be perfect as much as I go about it well am still Young and I start not up to a year

Leave a Reply