Loading

How to Create Registration Page in PHP/MySQL

Submitted by: 
Language: 
Visitors have accessed this post 193647 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 "simple_login".
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. <form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
  2. <table width="274" border="0" align="center" cellpadding="2" cellspacing="0">
  3. <tr>
  4. <td colspan="2">
  5. <div align="center">
  6. <?php
  7. $remarks=$_GET['remarks'];
  8. if ($remarks==null and $remarks=="")
  9. {
  10. echo 'Register Here';
  11. }
  12. if ($remarks=='success')
  13. {
  14. echo 'Registration Success';
  15. }
  16. ?>
  17. </div></td>
  18. </tr>
  19. <tr>
  20. <td width="95"><div align="right">First Name:</div></td>
  21. <td width="171"><input type="text" name="fname" /></td>
  22. </tr>
  23. <tr>
  24. <td><div align="right">Last Name:</div></td>
  25. <td><input type="text" name="lname" /></td>
  26. </tr>
  27. <tr>
  28. <td><div align="right">Gender:</div></td>
  29. <td><input type="text" name="mname" /></td>
  30. </tr>
  31. <tr>
  32. <td><div align="right">Address:</div></td>
  33. <td><input type="text" name="address" /></td>
  34. </tr>
  35. <tr>
  36. <td><div align="right">Contact No.:</div></td>
  37. <td><input type="text" name="contact" /></td>
  38. </tr>
  39. <tr>
  40. <td><div align="right">Picture:</div></td>
  41. <td><input type="text" name="pic" /></td>
  42. </tr>
  43. <tr>
  44. <td><div align="right">Username:</div></td>
  45. <td><input type="text" name="username" /></td>
  46. </tr>
  47. <tr>
  48. <td><div align="right">Password:</div></td>
  49. <td><input type="text" name="password" /></td>
  50. </tr>
  51. <tr>
  52. <td><div align="right"></div></td>
  53. <td><input name="submit" type="submit" value="Submit" /></td>
  54. </tr>
  55. </table>
  56. </form>

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. $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
  8. mysql_select_db($mysql_database, $bd) or die("Could not select database");
  9. ?>

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. include('connection.php');
  3. $fname=$_POST['fname'];
  4. $lname=$_POST['lname'];
  5. $mname=$_POST['mname'];
  6. $address=$_POST['address'];
  7. $contact=$_POST['contact'];
  8. $pic=$_POST['pic'];
  9. $username=$_POST['username'];
  10. $password=$_POST['password'];
  11. mysql_query("INSERT INTO member(fname, lname, gender, address, contact, picture, username, password)VALUES('$fname', '$lname', '$mname', '$address', '$contact', '$pic', '$username', '$password')");
  12. header("location: index.php?remarks=success");
  13. ?>

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"]["mname"].value;
  7. var d=document.forms["reg"]["address"].value;
  8. var e=document.forms["reg"]["contact"].value;
  9. var f=document.forms["reg"]["pic"].value;
  10. var g=document.forms["reg"]["pic"].value;
  11. var h=document.forms["reg"]["pic"].value;
  12. if ((a==null || a=="") && (b==null || b=="") && (c==null || c=="") && (d==null || d=="") && (e==null || e=="") && (f==null || f==""))
  13. {
  14. alert("All Field must be filled out");
  15. return false;
  16. }
  17. if (a==null || a=="")
  18. {
  19. alert("First name must be filled out");
  20. return false;
  21. }
  22. if (b==null || b=="")
  23. {
  24. alert("Last name must be filled out");
  25. return false;
  26. }
  27. if (c==null || c=="")
  28. {
  29. alert("Gender name must be filled out");
  30. return false;
  31. }
  32. if (d==null || d=="")
  33. {
  34. alert("address must be filled out");
  35. return false;
  36. }
  37. if (e==null || e=="")
  38. {
  39. alert("contact must be filled out");
  40. return false;
  41. }
  42. if (f==null || f=="")
  43. {
  44. alert("picture must be filled out");
  45. return false;
  46. }
  47. if (g==null || g=="")
  48. {
  49. alert("username must be filled out");
  50. return false;
  51. }
  52. if (h==null || h=="")
  53. {
  54. alert("password must be filled out");
  55. return false;
  56. }
  57. }
  58. </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

Thanks for the tutorial...its really nice....but for the picture uploading.....is it text or picture....am a little bit confused there....maybe you should please shed more light to that.....

Slimzy

Yeah, i would really like this portion to be treated. I am in need of the picture section to work well. Thank you. Cheers!

Guys, you don't store pictures in a database. You store the location of the picture, hence the datatype will be text. This doesn't resolve how to browse/upload/resize/store a picture, which I believe is beyond the scope of this tutorial. Hope this sheds some light on why the datatype is (and should be) text.

Nice, but the picture is in text datatype, and secondly no upload file input for the picture?it says text. This tutorial doesn't help in picture upload. but great work!

realy nice tutorial !!!!!!!!!!!

thanks for the same

Too many testing the other script. found error. but this code works perfect. im combine with login.
it just ALL PERFECT thanks. :)

I learn the concept easily through your coding. i got one error msg when i click for register link..

"Undefined index: remarks in C:\wamp\www\Simple_login\register.php on line 7"

remarks variable is not present in table. but i create that variable again i got error. help me to rectify. beginner to php mysql.

Thanks,

thank you so much for this tutorial, it works perfectly. I need this for a fourth year project. Took me less than 5 minutes to add it to my site. I love you!!

argie's picture

Thank you for your heart flattering comments, I'm glad i'd help you. i love you too :)

okay how do i get rid of coloums like all i want is username and password and i managed to mix it all around so username and password are required because before they weren't can u send me an entire code and the check code that is supposed to be in the head that works and is just Username: and Password: and the database code it would really help alot

Where are you pulling this info from?
$remarks=$_GET['remarks'];

An answer for this question?

i have copy paste all your code and upload with cpannel in my server
but when i put detail and submit it:NO ANY RESPONSE:??

wow, very helpful and precisely explained.

i dont get it.

Plese, I would like download all files in .zip/rar. Could you link the data here?

my form is not submitting values into the table..:(

do you mean when you try to submit it say success and when you check the DB and Table its still empty - - - - hahaha - - - same with me - - - - but i solved it immediately IDK maybe many ddnt notice or just ignore it NOTICE in creating the db the ARRANGMENT OF COLUMNS e.g memid,username,password . . . IT should be the same in "insert query" e.g mysql_query("INSERT INTO members (username, password,. . . .) VALUES ('$username', '$password', . . . . .)"); -------- get it.? thats how i solved it

Tried and Tied :( Please help me in solving these errors

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/xxx/public_html/zzz.com/new/code_exec.php:1) in /home/xxx/public_html/zzz.com/new/code_exec.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/xxx/public_html/zzz.com/new/code_exec.php:1) in /home/xxx/public_html/zzz.com/new/code_exec.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at /home/xxx/public_html/zzz.com/new/code_exec.php:1) in /home/xxx/public_html/zzz.com/new/code_exec.php on line 13

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/xxx/public_html/zzz.com/new/code_exec.php on line 14

he he

Im have problem with $remarks=$_GET['remarks'];

anybody got a solution to that ?

Same here can't get it to pst the right comment

Thanks

i have problem with remarks can you help me please

how about the primary ..??? where is the code to control it.???

error on remark what should i do?

Worked well For me...

Well compiled for a beginner...

yes i did it but i need also the code for logging in can you help me guys ? im about 1 week in trying and trying to code a register and log in i php. this is for my project . help me please?

Your codes were excellent but I'm experiencing error in $remarks=$_GET['remarks']. Can you please help me with this error? Thank you so much in advance.

Notice: Undefined index: remarks in C:\xampp\htdocs\index.php on line 76

You know... there are 25k+ people have looked at this page and I bet a lot have benefitted from this awesome nicely laid out tutorial and didn't bother to post a 'thank you'.

I'm not going to be one of them. So...

Thank you!!!!

i am getting a notice that undefined index:remarks
other than that all the code is working and registration is also successfull.please help in that error...

just replace this line in index.php:

7. $remarks=$_GET['remarks'];

with this instead.

7. if (!isset($_GET['remarks'])) {$remarks=""; }
else {$remarks=$_GET['remarks']; }

It tests to see if remarks is set. If it isn't, it will set it to empty.

thank you very much. . . :D

Thnx alot brother :)

thannks that cleared the error. but now have another problem Could not select database

change ur database name in connection.php to simple_login...where it is wrongly entered as registration.

thank you

thanks a lot... bro..

i tried this code.but i got errors.

hello

am newly about this php.am getting errors in the validation.

thanks a lot

Good

is it possible to sent registration information to a e-mail

javascript validation are not very safe and javascript can be disabled from the client pc so... validations can be bypassed

dude first in the article the database name is 'simple_login' but in the code you used 'registration'...... this might be tough for beginners....

Notice: Undefined index: remarks in C:\xampp\htdocs\index.php i got a error on remarks in index.php

I m not able to insert values into database :((
wat to do???

this is a marvelous!!

Thank you very much for the explanation! :)

Pages

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.

Filtered HTML

  • You may insert videos with [video:URL]
  • 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.
To prevent automated spam submissions leave this field empty.