Home >>Codeigniter Tutorial >CodeIgniter Insert Data into Database

CodeIgniter Insert Data into Database

CodeIgniter Insert Data into Database

CodeIgniter gives you access to a Query Builder class. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action. In this tutorial we will understand how to Save Records inside database using CodeIgniter Core Library. Here we are going to use student table to Save Records and Check user's existence. 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)


Copy the below given code in your User Controller.
class User extends CI_Controller 
	public function __construct()

	public function index()
		//first check user's email id already exists or not

//count number of matching rows
		$data['error']="<h3 style='color:red'>This user already exists</h3>";
		$data['error']="<h3 style='color:blue'>Your account created successfully</h3>";

Registration View

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

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

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

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