How to Create Registration Page in PHP/MySQL
Submitted by argie on Thursday, May 24, 2012 - 14:17.
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.
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
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.- CREATE TABLE IF NOT EXISTS `member` (
- `mem_id` int(11) NOT NULL AUTO_INCREMENT,
- `username` varchar(30) NOT NULL,
- `password` varchar(30) NOT NULL,
- `fname` varchar(30) NOT NULL,
- `lname` varchar(30) NOT NULL,
- `address` varchar(100) NOT NULL,
- `contact` varchar(30) NOT NULL,
- `picture` varchar(100) NOT NULL,
- `gender` varchar(10) NOT NULL,
- ) 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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Registration</title>
- </head>
- <body>
- <form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
- <table width="274" border="0" align="center" cellpadding="2" cellspacing="0">
- <tr>
- <td colspan="2">
- <div align="center">
- <?php
- // $remarks=$_GET['remarks'];
- {
- echo 'Register Here';
- }
- {
- echo 'Registration Success';
- }
- ?>
- </div></td>
- </tr>
- <tr>
- <td width="95"><div align="right">First Name:</div></td>
- <td width="171"><input type="text" name="fname" /></td>
- </tr>
- <tr>
- <td><div align="right">Last Name:</div></td>
- <td><input type="text" name="lname" /></td>
- </tr>
- <tr>
- <td><div align="right">Gender:</div></td>
- <td><input type="text" name="gender" /></td>
- </tr>
- <tr>
- <td><div align="right">Address:</div></td>
- <td><input type="text" name="address" /></td>
- </tr>
- <tr>
- <td><div align="right">Contact No.:</div></td>
- <td><input type="text" name="contact" /></td>
- </tr>
- <tr>
- <td><div align="right">Username:</div></td>
- <td><input type="text" name="username" /></td>
- </tr>
- <tr>
- <td><div align="right">Password:</div></td>
- <td><input type="text" name="password" /></td>
- </tr>
- <tr>
- <td><div align="right"></div></td>
- <td><input name="submit" type="submit" value="Submit" /></td>
- </tr>
- </table>
- </form>
- </body>
- </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.- <?php
- $mysql_hostname = "localhost";
- $mysql_user = "root";
- $mysql_password = "";
- $mysql_database = "registration";
- $prefix = "";
- $bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
- ?>
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.- <?php
- include('connection.php');
- $fname=$_POST['fname'];
- $lname=$_POST['lname'];
- $mname=$_POST['mname'];
- $address=$_POST['address'];
- $contact=$_POST['contact'];
- $username=$_POST['username'];
- $password=$_POST['password'];
- mysqli_query($bd, "INSERT INTO member(fname, lname, gender, address, contact, picture, username, password)VALUES('$fname', '$lname', '$mname', '$address', '$contact', '$pic', '$username', '$password')");
- ?>
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.- <script type="text/javascript">
- function validateForm()
- {
- var a=document.forms["reg"]["fname"].value;
- var b=document.forms["reg"]["lname"].value;
- var c=document.forms["reg"]["gender"].value;
- var d=document.forms["reg"]["address"].value;
- var e=document.forms["reg"]["contact"].value;
- var f=document.forms["reg"]["username"].value;
- var g=document.forms["reg"]["password"].value;
- if ((a==null || a=="") && (b==null || b=="") && (c==null || c=="") && (d==null || d=="") && (e==null || e==""))
- {
- alert("All Field must be filled out");
- return false;
- }
- if (a==null || a=="")
- {
- alert("First name must be filled out");
- return false;
- }
- if (b==null || b=="")
- {
- alert("Last name must be filled out");
- return false;
- }
- if (c==null || c=="")
- {
- alert("Gender name must be filled out");
- return false;
- }
- if (d==null || d=="")
- {
- alert("address must be filled out");
- return false;
- }
- if (e==null || e=="")
- {
- alert("contact must be filled out");
- return false;
- }
- if (f==null || f=="")
- {
- alert("username must be filled out");
- return false;
- }
- if (g==null || g=="")
- {
- alert("password must be filled out");
- return false;
- }
- }
- </script>
Comments
javascript
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'
dude first in the article the database name is 'simple_login' but in the code you used 'registration'...... this might be tough for beginners....
error to connect database
i am unable to connect database showing error----"Could not select database"
I GOT AN ERROR REMARKS IS AN
I GOT AN ERROR REMARKS IS AN UNDEFINED INDEX WHEN EXECUTING INDEX.PHP
Your saviour
Replace $remarks = $_GET['remarks'];
with
- $remarks = "";
- } else {
- $remarks = $_GET['remarks'];
- }
Absolutely dreadful tutorial.
Absolutely dreadful tutorial. Uses deprecated APIs, and is littered with bad practice and novice errors.
in one submit 45 rows created in database
in one sumit 45 rows created in database only one had a data why is that happening?
could not connect to database
Why this code cant connect to database? or should i say i cant connect to the database lol
if ($remarks==null and $remarks=="")
Nice work, I used it to implement into a database I had set up with logon already. I got errors for the if ($remarks==null and $remarks=="") line and found that the and needs to be changed to or. if ($remarks==null or $remarks=="") $remarks can't have two values at once so that if statement will never run.
Remarks error solved!
For those getting the 'remarks' error, this fix worked for me.
How can we make this remark appear on a single separate page?
Okay this worked perfectly for me, but it appears on top of the registration form (in other words, the registration form does not disappear when you submit the information details..They're still visible on the page..)
So, how do I get to make the registration form no longer visible, and only the message "The registration process was a success. Please proceed by Logging in, thank you!" appear on the page?
Please help shed some light on this matter, thanks in advance!
Check Duplicate Username
Hai, how to check the duplicate username already entered in the database.
Question??
Hey can you help me out??!! What is prefix and what do i enter there and also i get error "Could not select database"??
Thanks Man
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.
wow!!!!!!!!!!!!!!!!!!!!!!!!!!
whattupppp bro....u tutorial reallyx100 awsem.Ç!
like u site so much..thank a lot!!!!
I got this script to work for me after trail and error.
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.
The requested URL /SOLAI/index.php was not found on this server.
After i entering the fields, when i clicked the submit button, i got the error like this.
Page not found...
Undefined index: remarks in E:\xampp\htdocs\pho\index.php on lin
what that supposed to mean ??
mysql help
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
form validation
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
Validation
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.
Add new comment
- Add new comment
- 3285 views