This code gives you step by step explanation on how to update data in a table with CodeIgniter framework.
Model
Controller
View
Model
level.php
<?php
class Levels extends CI_Model {
public function selectdemodata_ind($id)
{
$this->load->database();
$query = $this->db->get_where('demo', array('id =' => $id));
return $query;
}
public function updatedemodata($data, $id)
{
$this->load->database();
$this->db->where('id', $id);
$this->db->update('demo', $data);
echo 'Data Updated successfully';
$this->load->helper('url');
redirect('/welcome/demodata');
}
}
?>
Controller
welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function updatedemodata(){
$id = $_GET['id'];
$data_one['k'] = $this->levels->selectdemodata_ind($id);
$this->load->view('updatedemodata', $data_one);
$this->load->model('levels');
$name = $this->input->post('name');
$number= $this->input->post('number');
$data = array(
'name' => $name,
'number' => $number
);
if(isset($_POST['update'])) {
if ((name !== '')) {
$id = $_GET['id'];
$this->levels->updatedemodata($data, $id);
} else {
echo 'Please fill fields';
}
}
}
}
?>
View
updatedemodata.php
<html>
<head>
<style>
.container {
width: 200px;
height: 100px;
padding: 200px;
}
label {
padding: 10px;
}
.btn {
float: right;
padding: 5px;
}
</style>
</head>
<?php foreach ($k->result() as $row) { ?>
<body class="container">
<form method='POST'>
<label>Name</label>
<input type="text" name="name"
value="<?php echo $row->name; ?>" ><br><br>
<label>Number</label>
<input type="text" name="number"
value="<?php echo $row->number; ?>" >
<input type="submit" class="btn"
name="update" value="update">
</form>
</body>
<?php } ?>
</html>
Model
Controller
View
Model
level.php
<?php
class Levels extends CI_Model {
public function selectdemodata_ind($id)
{
$this->load->database();
$query = $this->db->get_where('demo', array('id =' => $id));
return $query;
}
public function updatedemodata($data, $id)
{
$this->load->database();
$this->db->where('id', $id);
$this->db->update('demo', $data);
echo 'Data Updated successfully';
$this->load->helper('url');
redirect('/welcome/demodata');
}
}
?>
Controller
welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function updatedemodata(){
$id = $_GET['id'];
$data_one['k'] = $this->levels->selectdemodata_ind($id);
$this->load->view('updatedemodata', $data_one);
$this->load->model('levels');
$name = $this->input->post('name');
$number= $this->input->post('number');
$data = array(
'name' => $name,
'number' => $number
);
if(isset($_POST['update'])) {
if ((name !== '')) {
$id = $_GET['id'];
$this->levels->updatedemodata($data, $id);
} else {
echo 'Please fill fields';
}
}
}
}
?>
View
updatedemodata.php
<html>
<head>
<style>
.container {
width: 200px;
height: 100px;
padding: 200px;
}
label {
padding: 10px;
}
.btn {
float: right;
padding: 5px;
}
</style>
</head>
<?php foreach ($k->result() as $row) { ?>
<body class="container">
<form method='POST'>
<label>Name</label>
<input type="text" name="name"
value="<?php echo $row->name; ?>" ><br><br>
<label>Number</label>
<input type="text" name="number"
value="<?php echo $row->number; ?>" >
<input type="submit" class="btn"
name="update" value="update">
</form>
</body>
<?php } ?>
</html>
How to fetch data from a table in CodeIgniter?
http://www.phpjavascript.com/2018/02/how-to-fetch-data-from-table-in.html
How to insert data into a table with CodeIgniter
http://www.phpjavascript.com/2018/02/how-to-insert-data-in-table-with.html
http://www.phpjavascript.com/2018/02/how-to-fetch-data-from-table-in.html
How to insert data into a table with CodeIgniter
http://www.phpjavascript.com/2018/02/how-to-insert-data-in-table-with.html
When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove me from that service? Thanks a lot! data security
ReplyDelete