SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler 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!!

Mais conteúdo relacionado

Mais procurados

Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphismlalithambiga kamaraj
 
A Deep Dive into JSON-LD and Hydra
A Deep Dive into JSON-LD and HydraA Deep Dive into JSON-LD and Hydra
A Deep Dive into JSON-LD and HydraMarkus Lanthaler
 
Php famous built in functions
Php   famous built in functionsPhp   famous built in functions
Php famous built in functionsMaaz Shamim
 
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
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHPRamasubbu .P
 
Triton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON ChinaTriton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON ChinaWei-Bo Chen
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL QueriesAchievers Tech
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?Nikita Popov
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java Janu Jahnavi
 
Command Design Pattern
Command Design Pattern Command Design Pattern
Command Design Pattern anil kanzariya
 
Using Java to implement SOAP Web Services: JAX-WS
Using Java to implement SOAP Web Services: JAX-WS�Using Java to implement SOAP Web Services: JAX-WS�
Using Java to implement SOAP Web Services: JAX-WSKatrien Verbert
 
Servlet Hola Mundo con Eclipse y Tomcat
Servlet Hola Mundo con Eclipse y TomcatServlet Hola Mundo con Eclipse y Tomcat
Servlet Hola Mundo con Eclipse y Tomcatjubacalo
 
Chapter 07 php forms handling
Chapter 07   php forms handlingChapter 07   php forms handling
Chapter 07 php forms handlingDhani Ahmad
 
Php pattern matching
Php pattern matchingPhp pattern matching
Php pattern matchingJIGAR MAKHIJA
 
Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Hitesh Kumar
 

Mais procurados (20)

Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
A Deep Dive into JSON-LD and Hydra
A Deep Dive into JSON-LD and HydraA Deep Dive into JSON-LD and Hydra
A Deep Dive into JSON-LD and Hydra
 
Php famous built in functions
Php   famous built in functionsPhp   famous built in functions
Php famous built in functions
 
File Uploading in PHP
File Uploading in PHPFile Uploading in PHP
File Uploading 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
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
 
Triton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON ChinaTriton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON China
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Command Design Pattern
Command Design Pattern Command Design Pattern
Command Design Pattern
 
Using Java to implement SOAP Web Services: JAX-WS
Using Java to implement SOAP Web Services: JAX-WS�Using Java to implement SOAP Web Services: JAX-WS�
Using Java to implement SOAP Web Services: JAX-WS
 
DOM and Events
DOM and EventsDOM and Events
DOM and Events
 
Servlet Hola Mundo con Eclipse y Tomcat
Servlet Hola Mundo con Eclipse y TomcatServlet Hola Mundo con Eclipse y Tomcat
Servlet Hola Mundo con Eclipse y Tomcat
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Chapter 07 php forms handling
Chapter 07   php forms handlingChapter 07   php forms handling
Chapter 07 php forms handling
 
Php pattern matching
Php pattern matchingPhp pattern matching
Php pattern matching
 
Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++
 
Templates
TemplatesTemplates
Templates
 

Destaque

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
 

Destaque (15)

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
 
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
 

Semelhante a 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
 

Semelhante a 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
 

Mais de 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
 
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
 
MVC in PHP
MVC in PHPMVC in PHP
MVC in PHP
 

Último

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Último (20)

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

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!!