Update record in database using codeigniter
In this example we are going to show you how to update data into database using CodeIgniter framework in PHP.
Contents
- Download Codeigniter 3
- Create a new Database
- Create a new table
- Create a new project name
- Create a new controller file
- Create a new model file
- Create a new view file
The UPDATE statement is used to update records from a table:
Following Steps:
- Create a new table in Mysql database
- Project folder name is codeIgniter
- Database name is sample
- Create new controller file in Path: codeIgniter\application\controllers\Home.php
- Create new view file in Path: codeIgniter\application\views\list_data.php
- Create new view file in Path: codeIgniter\application\views\update_data.php
- Create new model file in Path: codeIgniter\application\models\Home_model.php
1. For creating table the SQL query is:
CREATE TABLE crud (`id` int(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,`first_name` varchar(30) NOT NULL,`last_name` varchar(30) NOT NULL,`email` varchar(30) NOT NULL,`mobile` varchar(30) NOT NULL);
2. Home.php (Controller)
<?phpclass Home 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('Home_model');}public function list_data(){$result['data'] = $this->Home_model->display_records();$this->load->view('display_records', $result);}/*Update*/public function update_data($id){/*Check submit button */if ($this->input->post('update')) {$data['first_name'] = $this->input->post('first_name');$data['last_name'] = $this->input->post('last_name');$data['email'] = $this->input->post('email');$data['mobile'] = $this->input->post('mobile');$this->Home_model->update_data($id, $data);echo "Update successfully";}/*load view form*/$result['data'] = $this->Home_model->display_ById($id);$this->load->view('update_data', $result);}}?>
3. list_data.php (View)
<!DOCTYPE html><html><head><title>Delete Data</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>Mobile</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>" . $row->mobile . "</td>";echo "<td><a href='http://localhost/codeIgniter/index.php/home/update_data/" . $row->id . "'>Update</a></td>";echo "</tr>";$i++;}?></table></body></html>
4. update_data.php (View)
<!DOCTYPE html><html><head><title>Registration form</title></head><body><form method="post" action="<?= base_url() ?>index.php/home/update_data/<?php echo $data->id; ?>"><table width="600" border="1" cellspacing="5" cellpadding="5"><tr><td width="230">First Name </td><td width="329"><input type="text" name="first_name" value="<?php echo $data->first_name; ?>" /></td></tr><tr><td>Last Name </td><td><input type="text" name="last_name" value="<?php echo $data->last_name; ?>" /></td></tr><tr><td>Email ID </td><td><input type="email" name="email" value="<?php echo $data->email; ?>" /></td></tr><tr><td>Mobile</td><td><input type="number" name="mobile" value="<?php echo $data->mobile; ?>" /></td></tr><tr><td colspan="2" align="center"><input type="submit" name="update" value="Update Data" /></td></tr></table></form></body></html>
5. Home_model.php (Model)
<?phpclass Home_model extends CI_Model{/*Display*/public function display_records(){$query = $this->db->get("crud");return $query->result();}public function display_ById($id){$this->db->select("*");$this->db->where("id", $id);$query = $this->db->get("crud");return $query - row();}public function update_data($id, $data){$this->db->where("id", $id);$this->db->update("crud", $data);return true;}}?>
Finally enter this url in your browser : http://localhost/codeIgniter/index.php/home/list_data
We hope this guide has been helpful. If you have any questions or need further assistance, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
0 Comments