Creating Registration Form with PHP and MySQLi

Creating Registration Form with PHP and MySQLi

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.

[button-blue url=”http://demos.eggslab.net/downloads/9″ target=”_blank” position=””]DOWNLOAD[/button-blue][button-green url=”http://demos.eggslab.net/registration-form-php-mysqli/” target=”_blank” position=””]DEMO[/button-green]

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

<form method="post" action="">
<fieldset>
<legend>Registration Form</legend>
<table width="400" border="0" cellpadding="10" cellspacing="10">
<tr>
<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>
</tr>
<tr>
<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>
</tr>
<tr>
<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>
</tr>
<tr>
<td height="23"></td>
<td><div align="right">
  <input type="submit" name="submit" value="Register!" />
</div></td>
</tr>
</table>
</fieldset>
</form>

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.

db.php

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

$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

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

if(isset($_POST["submit"]))
{
	//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..";
}
else
{
	//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')");

if($query)
{
	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.

[button-blue url=”http://demos.eggslab.net/downloads/9″ target=”_blank” position=””]DOWNLOAD[/button-blue][button-green url=”http://demos.eggslab.net/registration-form-php-mysqli/” target=”_blank” position=””]DEMO[/button-green]


19 responses to “Creating Registration Form with PHP and MySQLi”

  1. Umar Rauf Avatar

    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. Abdullah Majid Avatar

    Hi Umar, there is no field validation in this script except “email exist or not” validation. Please read the full article you will understand yourself. If you are still facing any problem we can talk at my abdullah@eggslab.net 🙂

  3. Umar Rauf Avatar

    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 😛

  4. Abdullah Majid Avatar

    Oh you are talking about that. Sorry, this is HTML5 input ‘s attribute required. You can read it here

  5. Umar Rauf Avatar

    Thanks a lot man, that really had me confused

  6. she2796 Avatar

    Much better if you will use prepared statements to avoid sql injections. 🙂

  7. Umar Rauf Avatar
    Umar Rauf

    Will try that also

  8. karthik Avatar
    karthik

    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

  9. michael Avatar
    michael

    thank you so much sir

  10. michael Avatar
    michael

    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

  11. snehakabra@gmail.com Avatar
    snehakabra@gmail.com

    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.

  12. Abdullah Majid Avatar

    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 🙂

    Cheers!

  13. sEiNZu Avatar
    sEiNZu

    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?

  14. Catalin Avatar
    Catalin

    How to add captcha on it??

  15. Jeffrey Avatar
    Jeffrey

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

    $password = md5($password);

    to

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

    I tested it and I will work 🙂

  16. Alkens Avatar
    Alkens

    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

  17. scorpion Avatar
    scorpion

    i have edited your code but it is not appearing on my database and the open window after submission

  18. Mike Avatar
    Mike

    How can I add hashing better than MD5 to this login script and the registration script?

  19. rohan patel Avatar
    rohan patel

    how can we change in required like this…….”try test@test.com as a email.”

    in which file we have to change ??

    thankx

Leave a Reply

Your email address will not be published. Required fields are marked *