CodeIgniter Tutorial: How To Update Data From Database Using CodeIgniter

This video learn how to update data from database using CodeIgniter.



1- Watch Related Video Before Start
https://www.youtube.com/watch?v=LGatnohVQK4

2- Update file autoload.php in "application/config"

Before 
$autoload['helper'] = array();

After
$autoload['helper'] = array('url', 'form', 'html');

3- Create file update_student.php for Contollers (Save to "application/controllers")

<?php

class update_student extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('update_student_model');
}

function show_student_id() {
$id = $this->uri->segment(3);
$data['students'] = $this->update_student_model->show_students();
$data['single_student'] = $this->update_student_model->show_student_id($id);
$this->load->view('update_student', $data);
}

function update_student_id1() {
$id= $this->input->post('sid');
$data = array(
'studentid' => $this->input->post('studentid'),
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'sex' => $this->input->post('sex')
);
$this->update_student_model->update_student_id1($id, $data);
$this->show_student_id();
redirect('student');
//echo 'You have been update this student.<br><a href=http://localhost/student/manage_student/>Go Back</a>';
}

}
?>

4- Create file update_student_model.php for Models (Save to "application/models")

<?php

class update_student_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('studentid', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
}

// Update Query For Selected Student
function update_student_id1($id, $data){
$this->db->where('studentid', $id);
$this->db->update('students', $data);
}
}
?>

5- Create file update_student.php for Views (Save to "application/views")

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Update Students</title>
</head>
<body>
<h1>Update Data</h1>
<?php foreach ($single_student as $student): ?>
<form method="post" action="http://localhost/student/student/update_student/">
<input type="hidden" name="sid" value="<?php echo $student->studentid; ?>">
<label>Student ID :</label><br>
<input type="text" name="studentid" value="<?php echo $student->studentid; ?>"><br><br>
<label>First Name :</label><br>
<input type="text" name="firstname" value="<?php echo $student->firstname; ?>"><br><br>
<label>Last Name :</label><br>
<input type="text" name="lastname" value="<?php echo $student->lastname; ?>"><br><br>
<label>Sex :</label><br>
<input type="text" name="sex" value="<?php echo $student->sex; ?>"><br><br>
<input type="submit" id="submit" name="dsubmit" value="Update">
</form>
<?php endforeach; ?>
    <br><a href='http://localhost/student/manage_student/'>Go Back</a>

</body>
</html>

6- Add link in file manage_student.php for Views

<td>
<a href='http://localhost/student/student/show/<?php echo $studentid; ?>'>Update</a>
</td>

7- Add route code with routes.php in "application/config"

$route['student'] = "manage_student";
$route['student/show/(:any)'] = 'update_student/show_student_id/$1';
$route['student/update_student'] = 'update_student/update_student_id1';

Demo
http://downloads.ziddu.com/download/24362137/student_update.zip.html

Read more blog:
http://video-tutorial-from-youtube.blogspot.com/

Follow Us with Facebook
https://www.facebook.com/pages/PHP-Tutorial/782239958529506

Share this

Related Posts

Previous
Next Post »