PHP

Creating a simple registration form using PHP and Mysql

Creating a simple registration form using PHP and Mysql
Share Deals

Here in this tutorial, we will learn how to create a Simple Registration form using PHP and Mysql. Users have to enter their details in the input fields. When users submit this form, the data will be stored in a database table. All data saved in the database can be used as user credentials. Here you have to follow 5 steps.

    1. Creating a database
    2. Creating a table in the database
    3. Creating a registration form
    4. Giving style effects to form
    5. Connecting to the database
    6. Submit data in the database

1. Creating a database

Firstly we need to create a database. To create the database, firstly login to PHPMyAdmin. After logging in click on the databases tab and in the input field enter the database name and click on the create database button. After creating the database move to the 2nd step.

2. Creating a table in the database

After creating the database, Create a table named members. Members table consists of id, username, email, password. Here, 

  •    The id is PRIMARY KEY and auto-incremented.
  •    The username field is used to store username.
  •    The email field is used to store the email address of the user.
  •    The password field is used to store the password of the user.

To create a table, execute the below code in SQL.

CREATE TABLE IF NOT EXISTS `members` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `username` varchar(100) NOT NULL,
 `email` varchar(200) NOT NULL,
 `password` varchar(100) NOT NULL,
 PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`) 
 );

3. Creating a registration form

The code given below is simple HTML code to generate a registration form. This form includes username, email, password fields, and a Register button. Save code below as index.php or signup.php

 <h1>Register Here</h1>
 <form action="" method="POST">
 <p>
  <label>User Name : </label>
  <input id="username" type="text" name="username" placeholder="username" />
 </p>
 <p>
  <label>E-Mail : </label>
  <input id="password" type="email" name="email" placeholder="name@email.com" />
   </p>
 <p>
  <label>Password : </label>
  <input id="password" type="password" name="password" placeholder="password" />
 </p>
 <input id="register" type="submit" name="submit" value="Register" />
</form>
<p> Copyright &copy; 2014 7topics.com</p>
</div>

4. Giving style effects to form

CSS code for the above HTML form to give it some stylish effect. Save this as style.css
 
 h1{
  font-family:'Open Sans',sans-serif !important;
 }
 #formbox{
  width: 500px;
  margin: 0 auto;
  text-align: center;
  padding: 10px;
  color: #fff;
  background : #556b2f;
  border-radius: 10px;
  -webkit-border-radius:10px;
  -moz-border-radius:10px;
 }
 #register{
  border:2px solid #008b8b;
  width: 100px;
  height:30px;
  color:#ffffff;
  padding-left: 8px;
  padding-right: 8px;
  background : #006400;
  border-radius: 10px;
  -webkit-border-radius:10px;
  -moz-border-radius:10px;
 }
 #register:hover{
  border:2px solid #088c8c;
  color:#ffffff;
  background : #006580;
 }

5. Connecting to the database

Here we will make a file config.php to connect to the database.

<?php
$connection = mysqli_connect('localhost', 'username', 'password');
if (!$connection){
 die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection,'database');
if (!$select_db){
 die("Database Selection Failed" . mysqli_error($connection));
}
?>

Here replace username, password, and database with your username, password, and database name.

6. Submit data in the database

The code given below is used to post data submitted by the user in the database.

<?php
require('config.php');
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])){
 $username = $_POST['username'];$email = $_POST['email'];
 $password = $_POST['password'];
 $query = "INSERT INTO `members` (username, password, email) VALUES ('$username', '$password', '$email')";
 $result = mysqli_query($connection,$query);
 if($result){
  $msg = "<b><font color='00ffff'>Your account is created successfully.</font></b>";
 }
}
?>

You can download the above source code by clicking here.

I hope this article helped you to learn user registration using PHP and MySQL. To learn more on related articles just explore our website and to get the latest news and updates follow me on Twitter & Facebook, subscribe to my newsletter. If you have any feedback please let us know by using the comment form.

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *