Create Registration Form using CodeIgniter and MySQL
In this post, We have to share How to Create a Registration Form in CodeIgniter and Bootstrap Framework along with MySQL Database and using MD5 is a 128-bit encryption algorithm. By using this users can easily register, username, first name, last name, email, password fields. After submitting the form, the data will be stored in a database table.This is a very simple example, you can just copy-paste and change according to your requirement.
Before started to implement the Create Registration using CodeIgniter, look files structure:
- create-registration-form-with-codeignite
- application
- config
- autoload.php
- constants.php
- routes.php
- models
- Users_model.php
- controllers
- Users.php
- views
- users
- thank-you.php
- index.php
- templates
- header.php
- footer.php
- users
- config
- system
- index.php
- assets
- images
- css
- style.css
- application
Step 1: Create MySQL Database and Table for User Registration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php CREATE TABLE `users` ( `user_id` int(12) NOT NULL, `name` varchar(50) DEFAULT NULL, `user_name` varchar(50) NOT NULL, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `status` int(1) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLE `users` ADD PRIMARY KEY (`user_id`), ADD UNIQUE KEY `email` (`email`); ALTER TABLE `users` MODIFY `user_id` int(12) NOT NULL AUTO_INCREMENT; COMMIT; ?> |
Step 2: Create a model file User Registration
Create a model file named “Users_model.php” inside “application/models” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?php /** * Description of Export Controller * * @author TechArise Team * * @email info@techarise.com */ defined('BASEPATH') or exit('No direct script access allowed'); class Users_model extends CI_Model { // declare private variable private $_userID; private $_name; private $_userName; private $_email; private $_password; private $_status; public function setUserID($userID) { $this->_userID = $userID; } public function setName($name) { $this->_name = $name; } public function setUserName($userName) { $this->_userName = $userName; } public function setEmail($email) { $this->_email = $email; } public function setPassword($password) { $this->_password = $password; } public function setStatus($status) { $this->_status = $status; } public function createUser() { $data = array( 'name' => $this->_name, 'email' => $this->_email, 'user_name' => $this->_userName, 'password' => $this->_password, 'status' => $this->_status, ); $this->db->insert('users', $data); return $this->db->insert_id(); } } ?> |
Step 3: Create a controller file User Registration
Next create a controller file named “Users.php” inside “application/controllers” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<?php /** * Description of Export Controller * * @author TechArise Team * * @email info@techarise.com */ defined('BASEPATH') or exit('No direct script access allowed'); class Users extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('Users_model', 'user'); } // Dashboard public function index() { $data['title'] = 'Dashboard - Tech Arise'; $data['metaDescription'] = 'Dashboard'; $data['metaKeywords'] = 'Dashboard'; $this->load->view('users/thank-you', $data); } // Register public function register() { $data['title'] = 'Register - Tech Arise'; $data['metaDescription'] = 'Register'; $data['metaKeywords'] = 'Register'; $this->load->view('users/index', $data); } // Action Register public function actionRegister() { $this->load->library('form_validation'); // field name, error message, validation rules $this->form_validation->set_rules('name', 'Name', 'required'); $this->form_validation->set_rules('username', 'User Name', 'trim|required|min_length[4]'); $this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email'); $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]'); $this->form_validation->set_rules('confirm_password', 'Password Confirmation', 'trim|required|matches[password]'); if ($this->form_validation->run() == FALSE) { $this->register(); } else { // post values $name = $this->input->post('name'); $username = $this->input->post('username'); $email = $this->input->post('email'); $password = $this->input->post('password'); // set post values $this->user->setName($name); $this->user->setUserName($username); $this->user->setEmail($email); $this->user->setPassword(MD5($password)); $this->user->setStatus(1); // insert values in database $this->user->createUser(); redirect('users/index'); } } } ?> |
Step 4: Create a view file for User Registration
Create a view file named “index.php” inside “application/views/users” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php $this->load->view('templates/header'); ?> <main role="main" class="container mcontainer"> <div class="wrapper"> <form action="<?php echo base_url(); ?>users/actionregister" method="post" id="loginform" name="Login_Form" class="form-signin" method="POST"> <h4 class="form-signin-heading">Create Registration Form with CodeIgniter</h4> <?php if (validation_errors()) { ?> <div class="alert alert-danger"> <?php echo validation_errors(); ?> </div> <?php } ?> <div class="form-group"> <label for="name">Name</label> <input type="text" class="form-control" id="name" name="name" placeholder="Name*" required /> </div> <div class="form-group"> <label for="name">User Name</label> <input type="text" class="form-control" id="user-name" name="username" placeholder="User Name*" required /> </div> <div class="form-group"> <label for="name">Email</label> <input type="email" class="form-control" id="email" name="email" placeholder="Email*" required /> </div> <div class="form-group"> <label for="name">Password</label> <input type="password" class="form-control" id="password" name="password" placeholder="Password" required /> </div> <div class="form-group"> <label for="exampleInputPassword1">Confirm Password</label> <input type="password" class="form-control" id="password-confirm" name="confirm_password" placeholder="Confirm Password" required /> </div> <button type="submit" class="btn btn-primary" style="float:right">Register</button> </form> </div> </main> <?php $this->load->view('templates/footer'); ?> |
Step 5: Create a Thank you page
Create a view file named “thank-you.php” inside “application/views/users” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php $this->load->view('templates/header'); ?> <main role="main" class="container mcontainer"> <div class="wrapper thank-you-page"> <div class="thank-you-pop"> <img src="<?php echo HTTP_IMAGES_PATH; ?>tick.png" alt=""> <h1>Thank You!</h1> <p>Your submission is received and we will contact you soon</p> </div> <hr> <div class="row"> <div class="col-lg-8 offset-2"> <a href="<?php echo base_url(); ?>register" class="btn btn-sm btn-primary" style="float:right"><i class="fa fa-user"></i> Register</a> <a class="btn btn-primary btn-sm" href="<?php echo base_url(); ?>create-registration-form-with-codeigniter-and-bootstrap/"><i class="fa fa-mail-reply"></i> Back To Tutorial</a> </div> </div> </div> </main> <?php $this->load->view('templates/footer'); ?> |
So open “application/config/routes.php” file and add code like as bellow:
1 2 3 4 |
<?php // routes $route['default_controller'] = 'users/register'; ?> |