How to create a registration page using PHP
Ever wonder how could you create a registration page for you website? If you have a website and you want your visitors to register for your mailing list for example, this tutorial is for you.
In this exercise we will create three files. Registration form, configuration file and a form to transact the information.
Registration form
<form name=”submitform” method=”post” action=”saveregistrant.php” >
<table width=”28%” border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td width=”41%”>Username</td>
<td width=”59%”><label>
<input type=”text” name=”username” id=”username” />
</label></td>
</tr>
<tr>
<td>Password</td>
<td><input type=”password” name=”password” id=”password” /></td>
</tr>
<tr>
<td>First Name</td>
<td><input type=”text” name=”firstname” id=”firstname” /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type=”text” name=”lastname” id=”lastname” /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><label>
<input type=”submit” name=”cmdsubmit” id=”cmdsubmit” value=”Submit” />
</label></td>
</tr>
</table>
</form>
This is very simple. I just made a table to hold all the objects so that we will have a beautiful form. Of course you can use divs to do that also. What are the properties in the form tag?
1. name = that is the name of the form. If there are multiple forms in you page, you might use this.
2. method = there are two methods in sending your data over the web. One is post another is get. The difference between the post and get is that when sending via post, the data will not be seen in the address bar unlike get method. To see more about their differences, read my other blog here.
3. action = this will tell the form where to go if submitted. You will specify here the file that will receive the data.
After having the registration form we will create saveregistrant.php
<?php
//connect to database
$host = “”; //your host. localhost if you are running your mysql in your computer
$user = “”; //your username to connection to the mysql
$pass = “”; //your password to connection to the mysql
$conn=mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($dbase,$conn) or die(“Cannot Connect”);
$usename    = $_POST['usename'];
$password    = $_POST['password'];
$firstname    = $_POST['firstname'];
$lastname    = $_POST['lastname'];
$sql = “INSERT INTO registrant (usename,password,firstname,lastname)
VALUES (‘$username’,'$password’,'$firstname’,'$lastname’)”
mysql_query($sql);
?>
The first 5 lines in the code above is for the database connection. In every project, big or small. These five lines are sometimes used in more than 1 page. To minimize time in typing these, what we mostly do is put that lines in another file and invoke it whenever we want to use it. Another good reason why we need to do that is if we will upload or migrate or files to another location or use another database connection, we will only change one file and not all files that have those lines.
To do this have a file like this
<?php
//connect to database
$host = “”; //your host. localhost if you are running your mysql in your computer
$user = “”; //your username to connection to the mysql
$pass = “”; //your password to connection to the mysql
$conn=mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($dbase,$conn) or die(“Cannot Connect”);
?>
and save it as config.php then modify your saveregistrant.php. Replace the five lines with this: include(‘config.php’);
Now you have a complete running module for your registration.
To download the files for this exercise, click here. I have also included a script to create a sample table.














Remember to always treat user input with suspicion. Escape data from $_POST before using it in SQL queries else you might end up with an SQL injection attack.
let us cover the SQL inject next time. Thanks for pointing that out tom