In our previous codeIgniter tutorials, we explained you, how to perform insertion and deletion operation on database using CodeIgniter. Now, this tutorial demonstrates you, how to perform Updation operation on database using CodeIgniter framework. Watch our live demo or download the update_ci.zip file from below link, extract files and include them in view, controller and model directory of your codeigniter framework as shown in the Read Me.txt file.
-: See Also :-
Insert Data using CodeIgniterÂ
Delete Data using CodeIgniterÂ
VIEW FILE: update_view.php
In this, we fetched all the names from data base and showed them in links. As user clicks on a particular name, its details appears in form on the right side with update button. User can click on Update button to Update his/her record in table.
<html>
<head>
<title>Update Data In Database Using CodeIgniter</title>
<link href='http://fonts.googleapis.com/css?family=Marcellus' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(). "css/update.css" ?>">
</head>
<body>
<div id="container">
<div id="wrapper">
<h1>Update Data In Database Using CodeIgniter </h1><hr/>
<div id="menu">
<p>Click On Menu</p>
<!-- Fetching Names Of All Students From Database -->
<ol>
<?php foreach ($students as $student): ?>
<li><a href="<?php echo base_url() . "index.php/update_ctrl/show_student_id/" . $student->student_id; ?>"><?php echo $student->student_name; ?></a></li>
<?php endforeach; ?>
</ol>
</div>
<div id="detail">
<!-- Fetching All Details of Selected Student From Database And Showing In a Form -->
<?php foreach ($single_student as $student): ?>
<p>Edit Detail & Click Update Button</p>
<form method="post" action="<?php echo base_url() . "index.php/update_ctrl/update_student_id1"?>">
<label id="hide">Id :</label>
<input type="text" id="hide" name="did" value="<?php echo $student->student_id; ?>">
<label>Name :</label>
<input type="text" name="dname" value="<?php echo $student->student_name; ?>">
<label>Email :</label>
<input type="text" name="demail" value="<?php echo $student->student_email; ?>">
<label>Mobile :</label>
<input type="text" name="dmobile" value="<?php echo $student->student_mobile; ?>">
<label>Address :</label>
<input type="text" name="daddress" value="<?php echo $student->student_address; ?>">
<input type="submit" id="submit" name="dsubmit" value="Update">
</form>
<?php endforeach; ?>
</div>
</div>
</div>
</body>
</html>
CONTROLLER FILE: update_ctrl.php
Copy the below file  in your controller directory.
<?php
class update_ctrl extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('update_model');
}
function show_student_id() {
$id = $this->uri->segment(3);
$data['students'] = $this->update_model->show_students();
$data['single_student'] = $this->update_model->show_student_id($id);
$this->load->view('update_view', $data);
}
function update_student_id1() {
$id= $this->input->post('did');
$data = array(
'Student_Name' => $this->input->post('dname'),
'Student_Email' => $this->input->post('demail'),
'Student_Mobile' => $this->input->post('dmobile'),
'Student_Address' => $this->input->post('daddress')
);
$this->update_model->update_student_id1($id,$data);
$this->show_student_id();
}
}
?>
MODELÂ FILE: update_model.php
Create new class in your model as shown below.
<?php
class update_model extends CI_Model{
// Function To Fetch All Students Record
function show_students(){
$query = $this->db->get('students');
$query_result = $query->result();
return $query_result;
}
// Function To Fetch Selected Student Record
function show_student_id($data){
$this->db->select('*');
$this->db->from('students');
$this->db->where('student_id', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
}
// Update Query For Selected Student
function update_student_id1($id,$data){
$this->db->where('student_id', $id);
$this->db->update('students', $data);
}
}
?>
My SQL Code Segment:
To create database and table, execute following codes in your My SQL .
CREATE DATABASE college;
CREATE TABLE students(
student_id int(10) NOT NULL AUTO_INCREMENT,
student_name varchar(255) NOT NULL,
student_email varchar(255) NOT NULL,
student_contact varchar(255) NOT NULL,
student_address varchar(255) NOT NULL,
PRIMARY KEY (student_id)
)
CSS FILE: update.css
Styling HTML Elements.
#container {
width:622px;
height:610px;
margin:50px auto
}
#wrapper {
width:520px;
padding:0 50px 20px;
background:linear-gradient(#fff,#AFEBD8);
border:1px solid #ccc;
box-shadow:0 0 5px;
font-family:'Marcellus',serif;
float:left;
margin-top:10px
}
#menu {
float:left;
width:200px;
margin-top:-30px
}
#detail {
float:left;
width:320px;
margin-top:-27px
}
a {
text-decoration:none;
color:blue
}
a:hover {
color:red
}
li {
padding:4px 0
}
h1 {
text-align:center;
font-size:28px
}
hr {
border:0;
border-bottom:1.5px solid #ccc;
margin-top:-10px;
margin-bottom:30px
}
#hide {
display:none
}
form {
margin-top:-40px
}
label {
font-size:17px
}
input {
width:100%;
padding:8px;
margin:5px 0 15px;
border:none;
box-shadow:0 0 5px
}
input#submit {
margin-top:10px;
font-size:18px;
background:linear-gradient(#22abe9 5%,#36caf0 100%);
border:1px solid #0F799E;
color:#fff;
font-weight:700;
cursor:pointer;
text-shadow:0 1px 0 #13506D
}
input#submit:hover {
background:linear-gradient(#36caf0 5%,#22abe9 100%)
}
p {
font-size:18px;
font-weight:700;
color:#18af0b
}
Conclusion: So, this was all about updating data in database using codeigniter framework. Keep following us to learn more.
32 Replies to “CodeIgniter Update Data In Database”
Your most welcome Rohini. Keep reading our blogs. 🙂
Thanks for the appreciation Nitish. Keep reading our blog. 🙂
Please send me link when ever you will add new posts in PHP.
Good Job !!!
Hey Deepanshu,
Here you can see our new blog posts :
https://www.formget.com/blog/
Very very ………nice…… If it will be an chain post then it would be better.
lot of thanx
Thanks for appreciation !
You can have a look into our new posts :
https://www.formget.com/blog/
nice tutorial.
Thanks for your appreciation …….!
Keep reading our other blog posts.
Nice. It helped me a lot.
Great tutorial! tks :))
In view
in controller
function functionname()
{
$id = $this->input->post(‘e_id’);
}
unable to get id what can do reply as soon as possible
Hello Ferry,
That sounds really great ! Keep reading our other blog posts for getting more coding tricks.
Catch up new posts from here
Regards,
FormGet Team
I am unable to load the view while running thIS URL: http://localhost/codeigniter/index.php/update_ctrl/update_student_id1.Please help me in this
students and single_student are undefined variables. That’s always the error whenever i run it
hi
thanks for your tutorials
but is does not work
with this url …index.php/update_ctrl/update_student_id1
i got this error
Fatal error: Call to undefined function base_url() in C:\wamp\www\codeigniter\CodeIgniter-2.2-stable\application\views\update_view.php on line 6 Call Stack #TimeMemoryFunctionLocation 10.0011386176{main}( )..\index.php:0 20.0033459344require_once( ‘C:\wamp\www\codeigniter\CodeIgniter-2.2-stable\system\core\CodeIgniter.php’ )..\index.php:202 30.02862598192call_user_func_array ( )..\CodeIgniter.php:359 40.02862598240update_ctrl->update_student_id1( )..\CodeIgniter.php:359 50.02952600464update_ctrl->show_student_id( )..\update_ctrl.php:26 60.03202707816CI_Loader->view( )..\update_ctrl.php:13 70.03202708376CI_Loader->_ci_load( )..\Loader.php:419 80.03252765392include( ‘C:\wamp\www\codeigniter\CodeIgniter-2.2-stable\application\views\update_view.php’ )..\Loader.php:833
i solve this but i got another error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$student_id
Filename: views/update_view.php
Line Number: 14
i have error with above code
so i wrote it again
try this too, it works
view file: view_update.php
………………….
Update Data In Database Using CodeIgniter
Student_Name;
?>
<a href="Student_id ; ?>”>
<form method="post" action="”>
<input type="hidden" name="did" value="Student_id; ?>”>
name:
<input type="text" name="dname" value="Student_Name; ?>”>
email:
<input type="text" name="demail" value="Student_Email; ?>”>
mobile:
<input type="text" name="dmobile" value="Student_Mobile; ?>”>
address:
<input type="text" name="daddress" value="Student_Address; ?>”>
…………………
controller file: update_ctrl.php
…………………..
load->library(‘form_validation’);
}
public function student_id(){
$this->load->model(‘update_model’);
$data[‘results’] = $this->update_model->readdb();
$id = $this->uri->segment(3);
$data[‘singlestudent’] = $this->update_model->show_student_id($id);
$this->load->view(‘update_view’, $data);
}
public function update_student(){
$this->form_validation->set_rules(‘dname’,’Name’,’required’);
$this->form_validation->set_rules(‘demail’,’Email’,’required|valid_email’);
if($this->form_validation->run()==FALSE){
$this->student_id();
}else{
$id = $this->input->post(‘did’);
$data = array(
‘Student_Name’ => $this->input->post(‘dname’),
‘Student_Email’ => $this->input->post(‘demail’),
‘Student_Mobile’ => $this->input->post(‘dmobile’),
‘Student_Address’ => $this->input->post(‘daddress’),
);
$this->load->model(‘update_model’);
$this->update_model->update_student($id,$data);
$this->student_id();
}
}
}
?>
………………………..
model file: update_model.php
………………………..
db->get(‘students’);
return $results->result();
}
function show_student_id($id){
$results = $this->db->get_where(‘students’,array(‘Student_id’=>$id));
return $results->result();
}
function update_student($id,$data){
$this->db->where(‘Student_id’ , $id);
$this->db->update(‘students’ , $data);
}
}
?>
website deleted lots of scrip, maybe for security
you should provide edit validation admin >
how should i run this update
I got this error
Message: Invalid argument supplied for foreach()
Thank you, it’s so helpfull
Thank You , Very Much. Developer 🙂
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$student_id
Filename: views/update_view.php
Line Number: 14 its very humble request to admin to other guy pls..give solution
Its working great thanks work perfect
Hello Adnan Khan,
Thanks for your appreciation …….!
Keep reading our other blog posts.
Thanks & Regards,
FormGet Team
it was really helpfully after hours of strugle….. thanks again
Thanks man you have really helped me a lot am bookmarking this page for future reference, nice article thou…… Kudoz
thanks fellas
Awesome tutorials!
Good going guys!
How i can fix this quotation error? Plz fix this error.
$var = “<a href='."id;?>”.’>Edit“;
what is [‘single_student’] here what will do this???????