How to Create Registration Page in PHP/MySQL

Submitted by: 
Language: 
Visitors have accessed this post 464597 times.

Related code: How to Create Secure Registration Page in PHP/MySQL

This is a simple tutorial that will teach you on how to create a simple registration form using PHP/MySQL with JavaScript for input validation. This tutorial will not teach you on how to create a good design but rather to give you knowledge on how to create a fully functional registration form.

Creating our Table

First we are going to create our database which stores our data.
To create a database:
1. Open phpmyadmin
2. Click create table and name it as simple_login.
3. Then name the database as "registration".
4. After creating a database name, click the SQL and paste the below code.

  1. CREATE TABLE IF NOT EXISTS `member` (
  2. `mem_id` int(11) NOT NULL AUTO_INCREMENT,
  3. `username` varchar(30) NOT NULL,
  4. `password` varchar(30) NOT NULL,
  5. `fname` varchar(30) NOT NULL,
  6. `lname` varchar(30) NOT NULL,
  7. `address` varchar(100) NOT NULL,
  8. `contact` varchar(30) NOT NULL,
  9. `picture` varchar(100) NOT NULL,
  10. `gender` varchar(10) NOT NULL,
  11. PRIMARY KEY (`mem_id`)
  12. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

Creating The Form

Next step is to create a form and save it as index.php. To create a form, open your HTML code editor and paste the code below after the tag.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Registration</title>
  6. </head>
  7. <body>
  8.  
  9. <form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
  10. <table width="274" border="0" align="center" cellpadding="2" cellspacing="0">
  11. <tr>
  12. <td colspan="2">
  13. <div align="center">
  14. <?php
  15. // $remarks=$_GET['remarks'];
  16. if (!isset($_GET['remarks']))
  17. {
  18. echo 'Register Here';
  19. }
  20. if (isset($_GET['remarks']) && $_GET['remarks']=='success')
  21. {
  22. echo 'Registration Success';
  23. }
  24. ?>
  25. </div></td>
  26. </tr>
  27. <tr>
  28. <td width="95"><div align="right">First Name:</div></td>
  29. <td width="171"><input type="text" name="fname" /></td>
  30. </tr>
  31. <tr>
  32. <td><div align="right">Last Name:</div></td>
  33. <td><input type="text" name="lname" /></td>
  34. </tr>
  35. <tr>
  36. <td><div align="right">Gender:</div></td>
  37. <td><input type="text" name="gender" /></td>
  38. </tr>
  39. <tr>
  40. <td><div align="right">Address:</div></td>
  41. <td><input type="text" name="address" /></td>
  42. </tr>
  43. <tr>
  44. <td><div align="right">Contact No.:</div></td>
  45. <td><input type="text" name="contact" /></td>
  46. </tr>
  47. <tr>
  48. <td><div align="right">Username:</div></td>
  49. <td><input type="text" name="username" /></td>
  50. </tr>
  51. <tr>
  52. <td><div align="right">Password:</div></td>
  53. <td><input type="text" name="password" /></td>
  54. </tr>
  55. <tr>
  56. <td><div align="right"></div></td>
  57. <td><input name="submit" type="submit" value="Submit" /></td>
  58. </tr>
  59. </table>
  60. </form>
  61. </body>
  62. </html>

Creating our Connection

Next step is to create a database connection and save it as "connection.php". This file is used to connect our form to database. This file serves as a bridge between our form and our database.

  1. <?php
  2. $mysql_hostname = "localhost";
  3. $mysql_user = "root";
  4. $mysql_password = "";
  5. $mysql_database = "registration";
  6. $prefix = "";
  7.  
  8. $bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
  9. mysqli_select_db($bd, $mysql_database) or die("Could not select database");
  10. ?>

Writing Our Save Script

Next step is to create our script that save our input data to database and save it as code_exec.php.

  1. <?php
  2.  
  3. include('connection.php');
  4.  
  5. $fname=$_POST['fname'];
  6. $lname=$_POST['lname'];
  7. $mname=$_POST['mname'];
  8. $address=$_POST['address'];
  9. $contact=$_POST['contact'];
  10. $username=$_POST['username'];
  11. $password=$_POST['password'];
  12.  
  13. mysqli_query($bd, "INSERT INTO member(fname, lname, gender, address, contact, picture, username, password)VALUES('$fname', '$lname', '$mname', '$address', '$contact', '$pic', '$username', '$password')");
  14.  
  15. header("location: index.php?remarks=success");
  16.  
  17. ?>

Validating The Input

To add some input validation using javascript, add the code below in the head tag of your index.php file. Input validation is used to make sure that all input field are filled out before saving the to database.

  1. <script type="text/javascript">
  2. function validateForm()
  3. {
  4. var a=document.forms["reg"]["fname"].value;
  5. var b=document.forms["reg"]["lname"].value;
  6. var c=document.forms["reg"]["gender"].value;
  7. var d=document.forms["reg"]["address"].value;
  8. var e=document.forms["reg"]["contact"].value;
  9. var f=document.forms["reg"]["username"].value;
  10. var g=document.forms["reg"]["password"].value;
  11. if ((a==null || a=="") && (b==null || b=="") && (c==null || c=="") && (d==null || d=="") && (e==null || e==""))
  12. {
  13. alert("All Field must be filled out");
  14. return false;
  15. }
  16. if (a==null || a=="")
  17. {
  18. alert("First name must be filled out");
  19. return false;
  20. }
  21. if (b==null || b=="")
  22. {
  23. alert("Last name must be filled out");
  24. return false;
  25. }
  26. if (c==null || c=="")
  27. {
  28. alert("Gender name must be filled out");
  29. return false;
  30. }
  31. if (d==null || d=="")
  32. {
  33. alert("address must be filled out");
  34. return false;
  35. }
  36. if (e==null || e=="")
  37. {
  38. alert("contact must be filled out");
  39. return false;
  40. }
  41. if (f==null || f=="")
  42. {
  43. alert("username must be filled out");
  44. return false;
  45. }
  46. if (g==null || g=="")
  47. {
  48. alert("password must be filled out");
  49. return false;
  50. }
  51. }
  52. </script>

That's it! You've been successfully created your simple registration form with javascript for the input validation.

Registration using php, input validation using javascript.

Update:

I have created another tutorial using a server side validation. And I am also using PDO to secure the registration page. Click here to see my latest update How to Create Registration Page in PHP/MySQL Using PDO Query.

Related code: How to Create Secure Registration Page in PHP/MySQL


Comments

Hello. I am ltonser. And i need to help. hmmm

Great help, thank you

Oh you are really programmer I want to be with you to develope my profession

very much thanks for codes

for real this good creating the registration form was my problem but i found the reality

hi, everything is working perfectly bt i discovered that the field are interchanged in the database, i.e the position of username, password... has shifted to the position of fname, lname... respectively which apply to the rest of the field. secondly, i can view my saved information, any observation?

sorry mam ,but i really please you to rather offer a zip download of source along with the text source

var f=document.forms["reg"]["pic"].value;

var g=document.forms["reg"]["pic"].value;

var h=document.forms["reg"]["pic"].value;

is in error!

Should be:

var f=document.forms["reg"]["pic"].value;
var g=document.forms["reg"]["username"].value;
var h=document.forms["reg"]["password"].value;

how to do that when I sign them so I threw the code_exec.php but that it held to php

thanx
sir i can get a knowledge

It's work great thanku so much

The requested URL /admin/admin_index.php was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

in face it is the best

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\nickz\code_exec.php:1) in C:\xampp\htdocs\nickz\code_exec.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\nickz\code_exec.php:1) in C:\xampp\htdocs\nickz\code_exec.php on line 2
Could not select database

What a bug-free code! Keep it up Argie.

This is what i have been searching for for long. thanks! It's so helpful

Wow Thank You really
Bless You ...!

please help me fully by explaining logout also and simply oin detail

hello Argie, thanks to you. succeed that I run your code. but, it appears some sentences like "Notice: Undefined index: remarks in C:\xampp\htdocs\test\index.php on line 10". what does that mean?
Thank you! Have a nice day!

Really nice tutorial, however, there is a huge bug! If you leave one of the fields empty and submit, a pop-up will appear saying you have to fill out the field. If you leave the field empty and submit it again the same pop-up will appear, but with the option to not show this warning again. If you check this option, press ok and submit again, you will register successfully even though the field is still empty!

Thank you for this article. it'll help me to connect my php form to database.
need more help in php
mail me at [email protected]

the record doesn't save in database ???

Really helpful for me

hmm...i also agree with you...

thanks for the code for this. Its much help.

Good Code. Works Perfectly.

your code is useful in our study so thanks again

how cn i make the code if i will going to load the form in listview the ID will be show

Hi there can you someone please help me on the validation part of this code, I keep trying to get to add the validation in to my index.php but it keeps failing to work. Where exactly do insert it because i tried head tags and does not work.

there is no need to validate all the fields once then start checking the value of each single text boxes.

Where your first checking result will be true the rest of course will be true too

i am getting an error

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in /home3/grabucom/public_html/connection.php on line 7
Could not connect database
help me man

Its really better for new guyes to put their efforts in coding.

After i entering the fields, when i clicked the submit button, i got the error like this.
Page not found...

how come on the more secure Login Page using the Md5 code even with a proper connection to the data base the darn login page can't find the username and password.

I've double and triple checked everything I can't solve the problem.

whattupppp bro....u tutorial reallyx100 awsem.Ç!
like u site so much..thank a lot!!!!

the post is fantastic

what about the "remarks" wats that???????

very helpfull thx

Its really helpful , Thanks alot.

it is very useful for my project

i have a little bit confusion...where is the location we retreive the remarks....i mean $_GET['remarks'].....what it means.....plz clear my doubt...and thanks for code.....

thanks my data store in database
thanks so much

thank for free education

THANK YOU VERY MUCH, MAY GOD RICHLY BLESS YOU. I AM BEEN SEARCHING ONLINE FOR A LONG TIME BUT NO AVAIL NOT UNTILL TODAY I CAME ACROSS YOUR POST, VERY PRECISE AND ACURATE. PLEASE CAN U HELP ME WITH SIGIN IN PAGE AGAIST THE REGISTRATION PAGE. THANK YOU.

Hey can you help me out??!! What is prefix and what do i enter there and also i get error "Could not select database"??

I see your code it so much good but I want to say that if you can please do it edit in some points and do for other language also
by thanks!!

where we add java script index.php page in a head tag its show the error

Hai, how to check the duplicate username already entered in the database.

For those getting the 'remarks' error, this fix worked for me.

  1. <?php
  2. if (!isset($_GET['remarks']))
  3. {
  4. $remarks="";
  5. }
  6. if (isset($_GET['remarks']))
  7. {
  8.  
  9. if($_GET['remarks']=='success')
  10. {
  11. echo '<p align="center">The registration process was a success. Please proceed by Logging in, thank you! <hr /></p>';
  12. }
  13. }
  14. ?>

Pages

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.