Codeigniter Tutorials
- What is codeigniter?
- Application_Architecture
- MVC Architecture
- HMVC Architecture
- Codeigniter Configuration
- Remove index.php from url in codeigniter
- MVC Concept
- View
- Alternate PHP Syntax for View Files
- Routing
- Codeigniter URL
- Get Current URL
- Previous page URL get
- Seo Friendly URL
- Slug Create in codeigniter
- What is _remap() function
- Remove controller name from url in codeigniter
- Codeigniter Controller Class
- Class Constructor
- GET $ POST method in Codeigniter
- Models
- Basepath, Apppath, FCPATH
- URI Segment
- Page Redirect
- Helper class
- Custom Helper class
- Form Helper
- Common Helper Functions
- Common Function
- Array Problems
- Call controller in Helper
- Add active class to menu using Helper class
- Custom Library
- Custom Library Example
- when to use get_instance()
- Codeigniter Hook
- how to work inline css in codeigniter
- Custom 404 page
- 404 custom error page
- Create custom config file in codeigniter
- How to set and get config item value
- How to Speed Up CodeIgniter App?
- Codeigniter Functions
- Session
- cookies
- How to Set & Get Tempdata in Codeigniter
- flash messages in Codeigniter
- Flashdata
- Encryption and Decryption In CodeIgniter
- Codeigniter security
- csrf token form security
- Password Hashing
- Form Validation
- Custom Validation
- Registration Form with validation
- Server Side Form Validation
- Validate Select Option Field
- Date Format Validation
- Date Format change in codeigniter
- Date Functions
- DOB Validation
- CI CRUD
- User SignUp
- User Login
- User Logout
- Login Account
- Login form with RememberMe
- Login Form with session
- User change password
- Change Password with Callback Validation to Check Old Password
- Forgot password
- Reset password
- Insert data in database
- Fetch data from database
- Update data in database
- Delete data in database
- File Upload
- Image Upload with resize Image
- Upload Multiple file and images
- Upload Multiple images with CRUD
- File and image update
- Upload Image Using Ajax.
- Email Send
- Email Send Using Email library
- Email Send Using SMTP Gmail
- Notification send
- store data in json format in DB
- Json parse
- Fetch data Using Ajax with Json data
- How to Show data Using Ajax with Json parse
- Get JSON Data from PHP Script using jQuery Ajax
- Insert data Using Ajax
- Submit data Using Ajax with form validation
- How to show data Using Ajax in codeigniter
- Insert & Update Using Ajax
- Registration Form With Validation Using Ajax in codeigniter
- Delete data Using Ajax Confirmation
- Delete All data Using checkbox selection
- Ajax CSRF Token
- Ajax Post
- Ajax serverside form validation
- Contact form using AJAX with form validation
- DataTable Using Ajax dynamically
- DataTables pagination using AJAX with Custom filter
- DataTables AJAX Pagination with Search and Sort in codeigniter
- DataTables in Codeigniter using Ajax
- Ajax Custom Serarch
- Ajax Live Data Search using Jquery PHP MySql
- Ajax Custom Serarch and sorting in datatable
- Dynamic Search Using Ajax
- Autocomplete using jquery ajax
- Jquery Ajax Autocomplete Search using Typeahead
- Dynamic Dependent Dropdown Using Ajax
- Dynamic Dependent Dropdown list Using Ajax
- Dynamic Dependent Dropdown in codeigniter using Ajax
- ajax username/email availability check using JQuery
- Check Email Availability Using Ajax
- Data Load on mouse scroll
- Ajax CI Pagination
- Pagination in codeigniter
- Ajax Codeigniter Pagination
- email exists or not using ajax with json
- CRUD using AJAX With Modal popup in CI
- Add / Show Data on modal popup using Ajax
- Modal popup Validation using Ajax
- Data show on Modal popup Using Ajax
- Add / Remove text field dynamically using jquery ajax
- How to Add/Delete Multiple HTML Rows using JavaScript
- Delete Multiple Rows using Checkbox
- Multiple Checkbox value
- Form submit using jquery Example
- REST & SOAP API
- Multi-Language implementation in CodeIgniter
- How to pass multiple array in view
- Captcha
- create zip file and download
- PhpOffice PhpSpreadsheet Library (Export data in excel sheet)
- data export in excel sheet
- Excel File generate in Codeigniter using PHPExcel
- Dompdf library
- tcpdf library
- Html table to Excel & docs download
- CI Database Query
- Database Query
- SQL Injection Prevention
- Auth Model
- Join Mysql
- Tree View in dropdown option list
- OTP Integration in codeigniter
- curl post
- download file using curl
- Sweet Alert
- Sweet alert Delete & Success
- Log Message in Codeigniter
- Menu & Submenu show dynamically
- Set Default value in input box
- Cron Jobs
- Stored Procedure
- Display Loading Image when AJAX call is in Progress
- Send SMS
- IP Address
- Codeigniter Tutorialspoint
- Website Link
- How To Create Dynamic Xml Sitemap In Codeigniter
- Paypal Payment Integration
- Get Latitude and Longitude From Address in Codeigniter Using google map API
- How To Create Simple Bar Chart In Codeigniter Using AmCharts?
- dynamic Highcharts in Codeigniter
- Barcode in Codeigniter
- Codeigniter Interview Questions
- Project
Home » Codeigniter »
How to update data in database?
VIEW FILE: update_view.php
<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
<?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
<?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
#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
}
Example :-
CREATE TABLE crud (
`id` int(11) NOT NULL,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(30) NOT NULL,
`email` varchar(30) NOT NULL,
PRIMARY KEY (id)
);
Crud.php (Controller file for Update) :-
<?php
class Crud extends CI_Controller
{
public function __construct()
{
//call CodeIgniter's default Constructor
parent::__construct();
//load database libray manually
$this->load->database();
//load Model
$this->load->model('Auth_model');
}
public function dispdata()
{
$result['data']=$this->Auth_model->display_records();
$this->load->view('display_records',$result);
}
public function updatedata()
{
$id=$this->input->get('id');
$result['data']=$this->Auth_model->displayrecordsById($id);
$this->load->view('update_records',$result);
if($this->input->post('update'))
{
$first_name=$this->input->post('first_name');
$last_name=$this->input->post('last_name');
$email=$this->input->post('email');
$this->Auth_model->update_records($first_name,$last_name,$email,$id);
echo "Date updated successfully !”;
}
}
}
?>
Auth_model.php :-
<?php
class Auth_model extends CI_Model
{
//Display
function display_records()
{
$query=$this->db->query("select * from crud");
return $query->result();
}
function displayrecordsById($id)
{
$query=$this->db->query("select * from form where id='".$id.”’”);
return $query->result();
}
//Update
function update_records($first_name,$last_name,$email,$id)
{
$query=$this->db->query("update form SET first_name='$first_name',last_name='$last_name',email='$email' where id='$id’”);
}
}
display_records.php (View file for fetch data) :-
<!DOCTYPE html>
<html>
<head>
<title>Delete records</title>
</head>
<body>
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr style="background:#CCC">
<th>Sr No</th>
<th>First_name</th>
<th>Last_name</th>
<th>Email Id</th>
<th>Update</th>
</tr>
<?php
$i=1;
foreach($data as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->first_name."</td>";
echo "<td>".$row->last_name."</td>";
echo "<td>".$row->email."</td>";
echo "<td><a href=‘updatedata?id=".$row->id."'>Update</a></td>";
echo "</tr>";
$i++;
}
?>
</table>
</body>
</html>
update_records.php (View file for update) :-
<!DOCTYPE html>
<html>
<head>
<title>Update Data</title>
</head>
<body>
<?php
$i=1;
foreach($data as $row)
{
?>
<form method="post">
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr>
<td width="230">Enter Your Name </td>
<td width="329"><input type="text" name="first_name" value="<?php echo $row->first_name; ?>"/></td>
</tr>
<tr>
<td>Enter Your Email </td>
<td><input type="text" name="last_name" value="<?php echo $row->last_name; ?>"/></td>
</tr>
<tr>
<td>Enter Your Mobile </td>
<td><input type="text" name="email" value="<?php echo $row->email; ?>"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="update" value="Update_Records"/></td>
</tr>
</table>
</form>
<?php } ?>
</body>
</html>
How to update single column in database
File name : controller.php
public function update_user_info()
{
$lid = $this->uri->segment(3);
$this->db->select('*');
$this->db->from('politician_info');
$this->db->where('id', $lid);
//$query = $this->db->get();
$query = $this->db->get()->row()->vote_count;
//echo $query;
$count = $query;
$upd = $count+1;
echo $upd;
$this->db->set('vote_count',$upd);
$this->db->where('id',$lid);
$this->db->update('politician_info');
}
Update single record
File name : controller.php
public function update_user_info()
{
$uid = $this->uri->segment(3);
// $uv = "updated value";
$this->db->set('vote_count',$uv);
$this->db->where('id',$uid);
$this->db->update('politician_info');
}
How to Update Record in Database.
View File
File name : index.php
<a href="<?php echo base_url();?>Action/edadmininfo/<?php echo $adminlist[$i]->id;?>">
<button class="btn btn-default btn-rounded btn-sm">
<span class="fa fa-pencil"></span>
</button>
</a>
Controller
File name : Action.php
<?php
defined('BASEPATH') OR exit('no direct access allowd');
class Action extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function edadmininfo()
{
$aid = $this->uri->segment(3);
$adminsresult = $this->Admin_model->get_admin_info($aid);
$data['editmodaldata'] = $adminsresult;
//print_r($data);
$this->load->view('admin_views/header');
$this->load->view('admin_views/leftpanel');
$this->load->view('admin_views/editadminview',$data);
$this->load->view('admin_views/footer');
}
}
Modal
File name : Auth_modal.php
public function get_admin_info($id)
{
$this->db->select('*');
$this->db->from('admin_info');
$this->db->where('id', $id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
View
File name : editadminview.php
<html>
<head>
</head>
<body>
<form action="<?php echo base_url();?>admin/action/update_admin_info" method="post" name="frm" id="frm">
<?php
for($i = 0; $i < count ( $editmodaldata ); ++ $i) {
?>
<input type="hidden" name="id" id="id" value="<?php echo $editmodaldata[$i]->id; ?>"/>
<input type="text" name="uid" id="uid" value="<?php echo $editmodaldata[$i]->user_id; ?>"/>
<input type="text" name="password" id="password" value="<?php echo $editmodaldata[$i]->password; ?>" />
<input type="text" name="email" id="email" value="<?php echo $editmodaldata[$i]->email; ?>"/>
<center>
<input type="submit" name="submit" value="Update"/>
</center>
<?php }?>
</form>
</body>
</html>
Controller
File name : Action.php
public function update_admin_info()
{
$id = $this->input->post('id');
$data = array(
'user_id' => $this->input->post('uid'),
'password' => $this->input->post('password'),
'email' => $this->input->post('email')
);
$this->Admin_model->upd_admininfo($data,$id);
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Your data is successfully submited.</div>');
redirect('Admininfo');
}
Modal
File name : Auth_modal.php
public function upd_admininfo($data,$id)
{
$this->db->where('id',$id);
$this->db->update('admin_info',$data);
}
How to update multiple column in database.
File name : Admin_model.php
public function update_info($id)
{
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
}
How to update multiple column in database.
File name : Admin_model.php
public function update_info($id)
{
$update_data = array();
$update_data['key-1'] = 'value-1';
$update_data['key-2'] = 'value-2';
.
.
.
$update_data['key-n'] = 'value-n';
$this->db->where('id', $id);
$query = $this->db->update('table_name', $update_data);
}
************** OR ***************
public function update_info($id)
{
$data = array(
'Designation' => 'Senior Accountant',
'Salary' => 192300
);
$this->db->where('EmpID', $id);
$this->db->update('Employees', $data);
}
********* OR ************
function update_product(){
$product_code=$this->input->post('product_code');
$product_name=$this->input->post('product_name');
$product_price=$this->input->post('price');
$this->db->set('product_name', $product_name);
$this->db->set('product_price', $product_price);
$this->db->where('product_code', $product_code);
$result=$this->db->update('product');
return $result;
}