Codeigniter User Registration Form with MySQL, Bootstrap

By | December 22, 2016

In this tutorial we will cover User registration Form using Codeginiter. We had previously covered Codeigniter Login System. So today we decided we will share codeigniter user registration tutorial. Codeigniter is one of the most popular frameworks in PHP. It makes web development very simpler and is open source. For this tutorial we will simply use view and controller. For designing UI we will use simple css and bootstrap. And for backend to store user details we will use MySQL database.

Codeigniter User Registration Form

Before we start make sure you edit database file which is in config folder. And make sure you name your database codeigniter_insert, since for this tutorial I have named my database after that name.

Lets go ahead and check codeigniter registration tutorial

Codeigniter User Registration Form with MySQL, Bootstrap

Database

CREATE DATABASE `codeigniter_insert` ;

CREATE TABLE IF NOT EXISTS `profile` (
`first_name` varchar(11) NOT NULL,
`last_name` varchar(55) NOT NULL,
`email` varchar(55) NOT NULL,
`password` varchar(55) NOT NULL
) ;

Codeigniter Code

register_view.php

I have created a file register_view.php which is a view file. It is designed using simple CSS and bootstrap. In this file we will add input fields for user registration. When user enter details and clicks on submit it will then call controller Register and inside Register controller it will then call function insert().


<!DOCTYPE html>
<html>
<head>
 <title></title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>



<style>

#main{

 position: absolute;
 top:200px;
 left:550px;
 width: 220px;
 padding: 10px;
 border: 3px solid green;
 margin: 1; 

}

</style>



</head>
<body>



<nav class="navbar navbar-default">



<div class="container-fluid">
 <!-- Brand and toggle get grouped for better mobile display -->



<div class="navbar-header">
 <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
 <span class="sr-only">Toggle navigation</span>
 <span class="icon-bar"></span>
 <span class="icon-bar"></span>
 <span class="icon-bar"></span>
 </button>
 <a class="navbar-brand" href="#">TechPlugg Dashboard</a>
 </div>





<ul class="nav navbar-nav navbar-right">
 </ul>



 </div>



<!-- /.navbar-collapse -->
 </div>



<!-- /.container-fluid -->
</nav>



<center>



<h1>Welcome to TechPlugg</h1>



We are a Tech Blog with Programming tutorials

</center>


<form enctype="multipart/form-data" action="<?php echo base_url(); ?>index.php/Register/insert" method="post" >


<div id ="main">
 <input type="text" name="first_name" placeholder="Enter first Name" />


 <input type="text" name="last_name" placeholder="Enter last Name"/>


 <input type="text" name="email" placeholder="Enter your Email ID" />


 <input type="password" name="password" placeholder="Enter a password"/>



 <center><input type="submit" value="Register" class="btn btn-primary"/></center>
 </form>

</div>


 </form>


 


</body>
</html>

Register.php

Register.php is a controller which will contains insert function. It is a user defined function. Function collects all the values submitted in the form and stores in data array and then further inserts into our table profile. After successful query it then redirects user to success_view.php file which will show message for successful user registration.

<?php class Register extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper(array('form', 'url'));
 }

 //index function
 function index()
 {
 //load file upload form
 $this->load->view('register_view');
 }

 
 function insert()
 {
 

 $first_name = $this->input->post('first_name');
 $last_name = $this->input->post('last_name');
 $email = $this->input->post('email');
 $password = md5(trim($this->input->post('password')));
 if($first_name !='' && $last_name !='' && $email !='' && $password!='')
 {
 $data = array('first_name'=>$first_name,'last_name'=>$last_name,'email'=>$email,'password'=>$password);
 if($this->db->insert('profile',$data))
 {
 
 redirect(base_url().'index.php/Register/success_register');
 
 } 
 } 
 else
 {
 redirect(base_url().'index.php/Register/');
 
 }
 }
 function success_register()
 {
 $this->load->view('success_register');
 } 
}
?>

 

success_view.php


<?php echo "Registration Successful"; ?>

<a href="<?php echo base_url(); ?>index.php/Register/">Register new account</a>

It simply shows success message. You can edit this file the way you like. It’s simply a message file.

 

You might like Image Uploading using Codeigniter.

I hope Codeigniter user registration was simple. If you want to ask any doubt please comment below. In the upcoming posts I will cover more such important codeigniter tutorials.