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.

DOWNLOADDEMO

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

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:

Now, connect your PHP Code with MySQL Database.

db.php

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

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.

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.

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.

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

DOWNLOADDEMO

15 COMMENTS

  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 🙂

    Cheers!

  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?

Leave a Reply