SlideShare a Scribd company logo
1 of 6
Download to read offline
Created by Vineet Kumar Saini
www.vineetsaini.wordpress.com
Add-Edit-Delete in Codeigniter in PHP
First of all we create a database i.e. company
CREATE DATABASE `company`;
Now we will create a table i.e. registration
CREATE TABLE `company`.`registration` (
`id` INT( 5 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 100 ) NOT NULL ,
`email` VARCHAR( 100 ) NOT NULL ,
`address` VARCHAR( 255 ) NOT NULL ,
`phone` VARCHAR( 12 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = InnoDB;
Now set the database name in the database.php file in codeigiter
Application folder -> config folder -> database.php
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'company';
Now we will create a controller i.e. emp.php in the controller folder
<?php
class emp extends CI_Controller
{
/* public function __construct() //php5 Constructor
{
parent::__construct();
$this->load->helper('url');
$this->load->model('emp_model');
}
*/ function emp() //php4 Constructor by class name
{
parent::CI_Controller();
$this->load->helper('url');
$this->load->model('emp_model');
}
function GetAll()
Created by Vineet Kumar Saini
www.vineetsaini.wordpress.com
{
$data['query']=$this->emp_model->emp_getall();
$this->load->view('emp_viewall',$data);
}
public function operation()
{
if(isset($_POST['btn']))
{
if(empty($_POST['id']))
{
$this->add_new_data();
}
else
{
$this->updating();
}
}
}
function add_new()
{
$this->load->view('form');
}
public function add_new_data()
{
$this->emp_model->add_data();
$this->GetAll();
}
public function update($id)
{
// $id=$this->input->get('id'); Get id from query string
$data['value']=$this->emp_model->get_data_id($id);
$this->load->view('form',$data);
}
public function updating()
{
$this->emp_model->update_data();
$this->GetAll();
}
public function delete($id)
{
//echo $id;exit;
$this->load->model('emp_model');
Created by Vineet Kumar Saini
www.vineetsaini.wordpress.com
$delete=$this->emp_model->delete_data($id);
$this->GetAll();
}
}
?>
Now we create a model emp_model.php in the model folder
<?php
class Emp_model extends CI_Model
{
function Emp_model()
{
parent::CI_Model();
$this->load->database();
}
function emp_getall()
{
$query=$this->db->get('registration');
return $query->result();
}
function add_data()
{
$name=$_POST['name'];
$email=$_POST['email'];
$address=$_POST['address'];
$phone=$_POST['phone'];
$value=array('name'=>$name,'email'=>$email,'address'=>$address,'phone'=>$phone);
$this->db->insert('registration',$value);
//ECHO "Succesfully Inserted?";
}
function delete_data($id)
{
$delete = "delete from registration where id='$id'";
$this->db->query($delete);
}
/* OR
function delete_data($id)
{
$this->db->delete('registration',array('id'=>$id));
}*/
function get_data_id($id)
Created by Vineet Kumar Saini
www.vineetsaini.wordpress.com
{
$query = $this->db->get_where('registration',array('id' => $id),1);
return $query;
}
function update_data()
{
$id=$_POST['id'];
$name=$_POST['name'];
$email=$_POST['email'];
$address=$_POST['address'];
$phone=$_POST['phone'];
$value= array('name'=>$name,'email'=>$email,'address'=>$address,'phone'=>$phone);
$this->db->update('registration',$value,array('id' => $id));
//ECHO "Succesfully Inserted?";
}
}
?>
Now we create a views i.e emp_viewall.php in the views folder
<center>
<h3>
<u>Display Data From Database Using Codeigniter in PHP</u></h3>
<table cellspacing="0" cellpadding="2" border="1" width="50%">
<tr>
<th>S.No</th>
<th>Name</th><th>Email</th><th>Address</th>
<th>phone</th><th>&nbsp;</th><th>&nbsp;</th>
</tr>
<?php
$i=1;
foreach($query as $row)
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->email; ?></td>
<td><?php echo $row->address; ?></td>
<td><?php echo $row->phone; ?></td>
<td><a href="<?php echo base_url().'/index.php/emp/update/'.$row->id;
Created by Vineet Kumar Saini
www.vineetsaini.wordpress.com
>">Edit</a></td>
<td><a href="<?php echo base_url().'/index.php/emp/delete/'.$row->id;
?>">Delete</a></td>
</tr>
<?php
$i++;
}
?>
<tr><td colspan="7"><a href="<?php echo base_url();?>/index.php/emp/add_new">Add
New</a></td></tr>
</table>
</br>
</center>
Now we create another views i.e form.php for updating data in the views folder
<?php
$name='';
$email='';
$address='';
$phone='';
$id='';
$submit='Add User';
if(isset($value) && !empty($value))
{
foreach($value->result() as $row)
{
$name=$row->name;
$email=$row->email;
$address=$row->address;
$phone=$row->phone;
$id=$row->id;
$submit='Update User';
}
}
?>
<html>
<head></head>
<body>
<form name="ajaxform" id="ajaxform" action="<?php echo base_url().'/index.php/emp/operation'?>"
method="POST">
<table border="1">
Created by Vineet Kumar Saini
www.vineetsaini.wordpress.com
<tr>
<th>Name</th>
<td><input type="text" name="name" value="<?php echo $name; ?>"/></td>
</tr>
<tr>
<th>Email </th>
<td><input type="text" name="email" value="<?php echo $email; ?>"/></td>
</tr>
<tr>
<th>Address</th>
<td><input type="text" name="address" value="<?php echo $address; ?>" /></td>
</tr>
<tr>
<th>Phone</th>
<td><input type="text" name="phone" value="<?php echo $phone; ?>" /></td>
</tr>
</table>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="hidden" id="btn" value="<?php echo $submit; ?>" name="btn"/>
<input type="submit" id="simple-post" value="<?php echo $submit; ?>" name="simple-post"/>
</form>
</body>
</html>
Output
Run the code using following url in your browser
http://localhost/projectname/index.php/emp/GetAll
Thanks!!

More Related Content

Viewers also liked

Country State City Dropdown in PHP
Country State City Dropdown in PHPCountry State City Dropdown in PHP
Country State City Dropdown in PHPVineet Kumar Saini
 
Top 100 PHP Interview Questions and Answers
Top 100 PHP Interview Questions and AnswersTop 100 PHP Interview Questions and Answers
Top 100 PHP Interview Questions and AnswersVineet Kumar Saini
 
PHP Technical Questions
PHP Technical QuestionsPHP Technical Questions
PHP Technical QuestionsPankaj Jha
 
Codeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate UriCodeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate UriAbdul Malik Ikhsan
 
Mysql Statments
Mysql StatmentsMysql Statments
Mysql StatmentsSHC
 
Codeigniter
CodeigniterCodeigniter
Codeignitershadowk
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xBo-Yi Wu
 
MS Access and Database Fundamentals
MS Access and Database FundamentalsMS Access and Database Fundamentals
MS Access and Database FundamentalsAnanda Gupta
 
RESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP FrameworkRESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP FrameworkBo-Yi Wu
 
Introduction to microsoft access
Introduction to microsoft accessIntroduction to microsoft access
Introduction to microsoft accessHardik Patel
 
MSU Food Fraud Initiative Emering Issues & New Frontiers for FDA Regulation_2014
MSU Food Fraud Initiative Emering Issues & New Frontiers for FDA Regulation_2014MSU Food Fraud Initiative Emering Issues & New Frontiers for FDA Regulation_2014
MSU Food Fraud Initiative Emering Issues & New Frontiers for FDA Regulation_2014Asian Food Regulation Information Service
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPBradley Holt
 
Ifp 1314-resumes-memoires-meef
Ifp 1314-resumes-memoires-meefIfp 1314-resumes-memoires-meef
Ifp 1314-resumes-memoires-meefwebmasterifp
 

Viewers also liked (16)

Dropdown List in PHP
Dropdown List in PHPDropdown List in PHP
Dropdown List in PHP
 
Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
 
Country State City Dropdown in PHP
Country State City Dropdown in PHPCountry State City Dropdown in PHP
Country State City Dropdown in PHP
 
Top 100 PHP Interview Questions and Answers
Top 100 PHP Interview Questions and AnswersTop 100 PHP Interview Questions and Answers
Top 100 PHP Interview Questions and Answers
 
PHP Technical Questions
PHP Technical QuestionsPHP Technical Questions
PHP Technical Questions
 
Codeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate UriCodeigniter : Custom Routing - Manipulate Uri
Codeigniter : Custom Routing - Manipulate Uri
 
Mysql Statments
Mysql StatmentsMysql Statments
Mysql Statments
 
Codeigniter
CodeigniterCodeigniter
Codeigniter
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.x
 
MS Access and Database Fundamentals
MS Access and Database FundamentalsMS Access and Database Fundamentals
MS Access and Database Fundamentals
 
RESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP FrameworkRESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP Framework
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
Introduction to microsoft access
Introduction to microsoft accessIntroduction to microsoft access
Introduction to microsoft access
 
MSU Food Fraud Initiative Emering Issues & New Frontiers for FDA Regulation_2014
MSU Food Fraud Initiative Emering Issues & New Frontiers for FDA Regulation_2014MSU Food Fraud Initiative Emering Issues & New Frontiers for FDA Regulation_2014
MSU Food Fraud Initiative Emering Issues & New Frontiers for FDA Regulation_2014
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Ifp 1314-resumes-memoires-meef
Ifp 1314-resumes-memoires-meefIfp 1314-resumes-memoires-meef
Ifp 1314-resumes-memoires-meef
 

Similar to Add edit delete in Codeigniter in PHP

Building secured wordpress themes and plugins
Building secured wordpress themes and pluginsBuilding secured wordpress themes and plugins
Building secured wordpress themes and pluginsTikaram Bhandari
 
テストデータどうしてますか?
テストデータどうしてますか?テストデータどうしてますか?
テストデータどうしてますか?Yuki Shibazaki
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutesBarang CK
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 MinutesAzim Kurt
 
Miniproject on Employee Management using Perl/Database.
Miniproject on Employee Management using Perl/Database.Miniproject on Employee Management using Perl/Database.
Miniproject on Employee Management using Perl/Database.Sanchit Raut
 
Moving a high traffic ZF1 Enterprise Application to SF2 - Lessons learned
Moving a high traffic ZF1 Enterprise Application to SF2 - Lessons learnedMoving a high traffic ZF1 Enterprise Application to SF2 - Lessons learned
Moving a high traffic ZF1 Enterprise Application to SF2 - Lessons learnedBaldur Rensch
 
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011Alessandro Nadalin
 
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móvilesDesarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móvilesLuis Curo Salvatierra
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7chuvainc
 
Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your CodeAbbas Ali
 
15. CodeIgniter editarea inregistrarilor
15. CodeIgniter editarea inregistrarilor15. CodeIgniter editarea inregistrarilor
15. CodeIgniter editarea inregistrarilorRazvan Raducanu, PhD
 
How else can you write the code in PHP?
How else can you write the code in PHP?How else can you write the code in PHP?
How else can you write the code in PHP?Maksym Hopei
 
WordPress as an application framework
WordPress as an application frameworkWordPress as an application framework
WordPress as an application frameworkDustin Filippini
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistenceHugo Hamon
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11Michelangelo van Dam
 

Similar to Add edit delete in Codeigniter in PHP (20)

Building secured wordpress themes and plugins
Building secured wordpress themes and pluginsBuilding secured wordpress themes and plugins
Building secured wordpress themes and plugins
 
テストデータどうしてますか?
テストデータどうしてますか?テストデータどうしてますか?
テストデータどうしてますか?
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutes
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
 
Miniproject on Employee Management using Perl/Database.
Miniproject on Employee Management using Perl/Database.Miniproject on Employee Management using Perl/Database.
Miniproject on Employee Management using Perl/Database.
 
Zero to SOLID
Zero to SOLIDZero to SOLID
Zero to SOLID
 
Moving a high traffic ZF1 Enterprise Application to SF2 - Lessons learned
Moving a high traffic ZF1 Enterprise Application to SF2 - Lessons learnedMoving a high traffic ZF1 Enterprise Application to SF2 - Lessons learned
Moving a high traffic ZF1 Enterprise Application to SF2 - Lessons learned
 
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móvilesDesarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móviles
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
 
Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your Code
 
15. CodeIgniter editarea inregistrarilor
15. CodeIgniter editarea inregistrarilor15. CodeIgniter editarea inregistrarilor
15. CodeIgniter editarea inregistrarilor
 
Presentation1
Presentation1Presentation1
Presentation1
 
Drupal7 dbtng
Drupal7  dbtngDrupal7  dbtng
Drupal7 dbtng
 
How else can you write the code in PHP?
How else can you write the code in PHP?How else can you write the code in PHP?
How else can you write the code in PHP?
 
Database api
Database apiDatabase api
Database api
 
WordPress as an application framework
WordPress as an application frameworkWordPress as an application framework
WordPress as an application framework
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11
 
Separation of concerns - DPC12
Separation of concerns - DPC12Separation of concerns - DPC12
Separation of concerns - DPC12
 

More from Vineet Kumar Saini (20)

Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHP
 
Introduction to Html
Introduction to HtmlIntroduction to Html
Introduction to Html
 
Computer Fundamentals
Computer FundamentalsComputer Fundamentals
Computer Fundamentals
 
Stripe in php
Stripe in phpStripe in php
Stripe in php
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Install Drupal on Wamp Server
Install Drupal on Wamp ServerInstall Drupal on Wamp Server
Install Drupal on Wamp Server
 
Joomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDFJoomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDF
 
Functions in PHP
Functions in PHPFunctions in PHP
Functions in PHP
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Update statement in PHP
Update statement in PHPUpdate statement in PHP
Update statement in PHP
 
Delete statement in PHP
Delete statement in PHPDelete statement in PHP
Delete statement in PHP
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
 
Types of Error in PHP
Types of Error in PHPTypes of Error in PHP
Types of Error in PHP
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
Database connectivity in PHP
Database connectivity in PHPDatabase connectivity in PHP
Database connectivity in PHP
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Browser information in PHP
Browser information in PHPBrowser information in PHP
Browser information in PHP
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Variables in PHP
Variables in PHPVariables in PHP
Variables in PHP
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 

Recently uploaded (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 

Add edit delete in Codeigniter in PHP

  • 1. Created by Vineet Kumar Saini www.vineetsaini.wordpress.com Add-Edit-Delete in Codeigniter in PHP First of all we create a database i.e. company CREATE DATABASE `company`; Now we will create a table i.e. registration CREATE TABLE `company`.`registration` ( `id` INT( 5 ) NOT NULL AUTO_INCREMENT , `name` VARCHAR( 100 ) NOT NULL , `email` VARCHAR( 100 ) NOT NULL , `address` VARCHAR( 255 ) NOT NULL , `phone` VARCHAR( 12 ) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB; Now set the database name in the database.php file in codeigiter Application folder -> config folder -> database.php $db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; $db['default']['password'] = ''; $db['default']['database'] = 'company'; Now we will create a controller i.e. emp.php in the controller folder <?php class emp extends CI_Controller { /* public function __construct() //php5 Constructor { parent::__construct(); $this->load->helper('url'); $this->load->model('emp_model'); } */ function emp() //php4 Constructor by class name { parent::CI_Controller(); $this->load->helper('url'); $this->load->model('emp_model'); } function GetAll()
  • 2. Created by Vineet Kumar Saini www.vineetsaini.wordpress.com { $data['query']=$this->emp_model->emp_getall(); $this->load->view('emp_viewall',$data); } public function operation() { if(isset($_POST['btn'])) { if(empty($_POST['id'])) { $this->add_new_data(); } else { $this->updating(); } } } function add_new() { $this->load->view('form'); } public function add_new_data() { $this->emp_model->add_data(); $this->GetAll(); } public function update($id) { // $id=$this->input->get('id'); Get id from query string $data['value']=$this->emp_model->get_data_id($id); $this->load->view('form',$data); } public function updating() { $this->emp_model->update_data(); $this->GetAll(); } public function delete($id) { //echo $id;exit; $this->load->model('emp_model');
  • 3. Created by Vineet Kumar Saini www.vineetsaini.wordpress.com $delete=$this->emp_model->delete_data($id); $this->GetAll(); } } ?> Now we create a model emp_model.php in the model folder <?php class Emp_model extends CI_Model { function Emp_model() { parent::CI_Model(); $this->load->database(); } function emp_getall() { $query=$this->db->get('registration'); return $query->result(); } function add_data() { $name=$_POST['name']; $email=$_POST['email']; $address=$_POST['address']; $phone=$_POST['phone']; $value=array('name'=>$name,'email'=>$email,'address'=>$address,'phone'=>$phone); $this->db->insert('registration',$value); //ECHO "Succesfully Inserted?"; } function delete_data($id) { $delete = "delete from registration where id='$id'"; $this->db->query($delete); } /* OR function delete_data($id) { $this->db->delete('registration',array('id'=>$id)); }*/ function get_data_id($id)
  • 4. Created by Vineet Kumar Saini www.vineetsaini.wordpress.com { $query = $this->db->get_where('registration',array('id' => $id),1); return $query; } function update_data() { $id=$_POST['id']; $name=$_POST['name']; $email=$_POST['email']; $address=$_POST['address']; $phone=$_POST['phone']; $value= array('name'=>$name,'email'=>$email,'address'=>$address,'phone'=>$phone); $this->db->update('registration',$value,array('id' => $id)); //ECHO "Succesfully Inserted?"; } } ?> Now we create a views i.e emp_viewall.php in the views folder <center> <h3> <u>Display Data From Database Using Codeigniter in PHP</u></h3> <table cellspacing="0" cellpadding="2" border="1" width="50%"> <tr> <th>S.No</th> <th>Name</th><th>Email</th><th>Address</th> <th>phone</th><th>&nbsp;</th><th>&nbsp;</th> </tr> <?php $i=1; foreach($query as $row) { ?> <tr> <td><?php echo $i; ?></td> <td><?php echo $row->name; ?></td> <td><?php echo $row->email; ?></td> <td><?php echo $row->address; ?></td> <td><?php echo $row->phone; ?></td> <td><a href="<?php echo base_url().'/index.php/emp/update/'.$row->id;
  • 5. Created by Vineet Kumar Saini www.vineetsaini.wordpress.com >">Edit</a></td> <td><a href="<?php echo base_url().'/index.php/emp/delete/'.$row->id; ?>">Delete</a></td> </tr> <?php $i++; } ?> <tr><td colspan="7"><a href="<?php echo base_url();?>/index.php/emp/add_new">Add New</a></td></tr> </table> </br> </center> Now we create another views i.e form.php for updating data in the views folder <?php $name=''; $email=''; $address=''; $phone=''; $id=''; $submit='Add User'; if(isset($value) && !empty($value)) { foreach($value->result() as $row) { $name=$row->name; $email=$row->email; $address=$row->address; $phone=$row->phone; $id=$row->id; $submit='Update User'; } } ?> <html> <head></head> <body> <form name="ajaxform" id="ajaxform" action="<?php echo base_url().'/index.php/emp/operation'?>" method="POST"> <table border="1">
  • 6. Created by Vineet Kumar Saini www.vineetsaini.wordpress.com <tr> <th>Name</th> <td><input type="text" name="name" value="<?php echo $name; ?>"/></td> </tr> <tr> <th>Email </th> <td><input type="text" name="email" value="<?php echo $email; ?>"/></td> </tr> <tr> <th>Address</th> <td><input type="text" name="address" value="<?php echo $address; ?>" /></td> </tr> <tr> <th>Phone</th> <td><input type="text" name="phone" value="<?php echo $phone; ?>" /></td> </tr> </table> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <input type="hidden" id="btn" value="<?php echo $submit; ?>" name="btn"/> <input type="submit" id="simple-post" value="<?php echo $submit; ?>" name="simple-post"/> </form> </body> </html> Output Run the code using following url in your browser http://localhost/projectname/index.php/emp/GetAll Thanks!!