Web Info and IT Lessons Blog...

Wednesday 18 February 2015

Simple user registration interface in PHP and MySql

The previous lesson of PHP Lessons Series was about MySql Database Connection in PHP and in this lesson we will learn to create a simple user registration interface using PHP and MySql.

Simple User Registration Interface

User registration process in PHP requires an html form to fill the registration data, PHP to process the filled data and Database to store the user data. The first thing we need to do here is make a simple registration form using html form elements. Consider a simple html form given below:


<form action="#" method="GET">
<table>
<tr>
<td>Firstname:</td><td><input type="text" name="firstname" /></td>
</tr>
<tr>
<td>Lastname:</td><td><input type="text" name="lastname" /></td>
</tr>
<tr>
<td>Email:</td><td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Password:</td><td><input type="password" name="password" /></td>
</tr>
<tr>
<td>Confirm Pass:</td>
<td><input type="password" name="confirm_pass" /></td>
</tr>
<tr>
<td> </td><td><input type="submit" value="Submit"/></td><br />
</tr>
</table>
</form>


The above registration form submits data to PHP using GET method. We can also send the form data using POST method if we want to, all we have to do is change the method in the above form to "POST". Now in the next step we will receive the form data using PHP globals $_GET. Consider the PHP code given below:


if(isset($_GET['submit'])){
 $firstname = $_GET['firstname'];
 $lastname = $_GET['lastname'];
 $email = $_GET['email'];
 $password = $_GET['password'];
 $confirm_pass = $_GET['confirm_pass'];
 
 echo 'First name is : '.$firstname.'<br />';
 echo 'Last name is : '.$lastname.'<br />';
 echo 'Email is : '.$email.'<br />';
 echo 'Password is : '.$password.'<br />';
}


The above PHP code checks if the submit button is clicked and if so, it stores the form data contained in the GET array in local variables and finally echos the received data. You can see in the above code the key specified in the GET array is the same as the name of the form field we want to fetch.

So, now at this stage we have all the data available in PHP that we need to store in the database. In the next step, we will create a user table in MySql to save the user data. Consider the user table shown in the image below:

User Registration Table

Now all we need to do is insert data in the user table using MySql Insert Query but before that make sure you have your database connection file included using include or require statement at the top of your index file so that the connection to database is already made before executing the MySql query in PHP.

The final PHP code to get and insert the form data in the database using mysql is given below:


include 'dbconnection.php';

if(isset($_GET['submit'])){
 $firstname = $_GET['firstname'];
 $lastname = $_GET['lastname'];
 $email = $_GET['email'];
 $password = $_GET['password'];
 $confirm_pass = $_GET['confirm_pass'];
 
 $sql = "INSERT INTO user VALUES('','".$firstname."','".$lastname."',
               '".$email."','".$password."','".$confirm_pass."')";
 if(mysql_query($sql)){
  echo "Records added successfully.";
 }
 
}


The final PHP code to get and insert the form data in the database using mysqli is given below:


include 'dbconnection.php';

if(isset($_GET['submit'])){
 $firstname = $_GET['firstname'];
 $lastname = $_GET['lastname'];
 $email = $_GET['email'];
 $password = $_GET['password'];
 $confirm_pass = $_GET['confirm_pass'];
 
 $sql = "INSERT INTO user VALUES('','".$firstname."','".$lastname."',
               '".$email."','".$password."','".$confirm_pass."')";
 if(mysqli_query($connection, $sql)){
  echo "Records added successfully.";
 }
 
}


The final PHP code to get and insert the form data in the database using PDO is given below:


include 'dbconnection.php';

if(isset($_GET['submit'])){
 $firstname = $_GET['firstname'];
 $lastname = $_GET['lastname'];
 $email = $_GET['email'];
 $password = $_GET['password'];
 $confirm_pass = $_GET['confirm_pass'];
 
 $sql = "INSERT INTO user VALUES('','".$firstname."','".$lastname."',
               '".$email."','".$password."','".$confirm_pass."')";
 $connection->exec($sql);
    echo "Records added successfully";
 
}


For dbConnection file check this lesson.

No comments:

Post a Comment