Home >>Codeigniter Tutorial >Create registration form in codeigniter

Create registration form in codeigniter

Create registration form in codeigniter

In this tutorial we will understand how to create simple registration form in CodeIgniter. Before inserting Students data inside database we will check student email id is already exists then show error message "User already exists" if not already exists then store information inside database. We will use student table to save data. This is student table structure

Table Name : student
student_id int primary key auto_increment
name char(50)
email varchar(100)
password varchar(100)
mobile bigint
course char(100)

Controller

Copy the below given code in your User Controller. In this example we have not used proper model page , all database query wrote in Controller.

<?php
class User extends CI_Controller 
{
	public function __construct()
	{
	parent::__construct();
	$this->load->database();
	$this->load->helper('url');
	}

	public function index()
	{
		
		if($this->input->post('register'))
		{
		$n=$this->input->post('name');
		$e=$this->input->post('email');
		$p=$this->input->post('pass');
		$m=$this->input->post('mobile');
		$c=$this->input->post('course');
		
		$que=$this->db->query("select * from student where email='".$e."'");
		$row = $que->num_rows();
		if($row)
		{
		$data['error']="<h3 style='color:red'>This user already exists</h3>";
		}
		else
		{
		$que=$this->db->query("insert into student values('','$n','$e','$p','$m','$c')");
		
		$data['error']="<h3 style='color:blue'>Your account created successfully</h3>";
		}			
				
		}
	$this->load->view('student_registration',@$data);	
	}
}
?>

Registration View

Copy the below given code in your student_registration.php View Page.

<!DOCTYPE html>
<html>
<head>
<title>Student Registration form</title>
</head>

<body>
	<form method="post">
		<table width="600" align="center" border="1" cellspacing="5" cellpadding="5">
	<tr>
		<td colspan="2"><?php echo @$error; ?></td>
	</tr>	
  <tr>
    <td width="230">Enter Your Name </td>
    <td width="329"><input type="text" name="name"/></td>
  </tr>
  
  <tr>
    <td>Enter Your Email </td>
    <td><input type="text" name="email"/></td>
  </tr>
  
  <tr>
    <td>Enter Your Password </td>
    <td><input type="password" name="pass"/></td>
  </tr>

  <tr>
    <td>Enter Your Mobile </td>
    <td><input type="text" name="mobile"/></td>
  </tr>
  
  <tr>
    <td>Select Your Course </td>
    <td>
	<select name="course">
		<option value="">Select Course</option>
		<option>PHP</option>
		<option>Java</option>
		<option>Wordpress</option>
	</select>
	</td>
  </tr>
  
  <tr>
    <td colspan="2" align="center">
	<input type="submit" name="register" value="Register Me"/></td>
  </tr>
</table>

	</form>
</body>
</html>

Execute User Controller’s index method

Open your web browser and Pass : http://localhost/CodeIgniter/index.php/user/index

Output(Save Records if email id is not already exists)
Output(Show error "This user already exists")

No Sidebar ads