SIGN UP LOGIN

Creating a simple registration form using PHP and Mysql

  25th Dec, 2019   Rahul Ranjan   0 comment
Creating a simple registration form using PHP and Mysql

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

    1. Creating a database
    2. Creating table in database with column
      id
      username
      email
      password
    3. Creating registration form
    4. Giving style effects to form
    5. Connecting to the database
    6. 6.Submit data in database

1. Creating a database

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

2. Creating table in database with column

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

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

To create table , execute 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 registration form

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 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 database

Code given below is used to post data submitted by user in 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 latest news and updates follow me on twitter & facebook, subscribe to my newsletter. If you have any feedback please let us know by using comment form.

Rahul Ranjan
Author: Rahul Ranjan

Founder of 7topics. Rahul Ranjan is a website developer and blogger who loves to read and share about new techonology with others through his blog.

SUBSCRIBE TO NEWSLETTER

Enter your email address to subscribe to this blog and receive notifications of new posts by email

RECENT POSTS

1