In this example we using Models, Views, Controller Structure for Update the inserted data.
To update data in the database first we have to create a Controller file.
controller/index.php
<?php
class Index extends Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->view->allrecords = $this->model->getAllrecords();
$this->view->render('index/index');
}
function edit_index()
{
$data = $_GET;
if($_GET['id']=='')
{
$this->view->pick='';
$this->view->data=$data;
}
else
{
$this->view->pick=$_GET['id'];
$this->view->content= $this->model->getOnerecord($_GET['id']);
}
$this->view->render('index/edit_index');
}
function edit_submit_index(){
$arg=$_POST['id'];
$data = array(
'name' =>$_POST['name'],
'email' =>$_POST['email'],
'contact' => $_POST['contact']
);
$this->model->edit_submit_index($data,$arg);
}
header('location: index');
}
Here is the model file which we are using for Updating the inserted data to database.
The file index_model.php is created under Models folder
models/index_model.php
<?php
class Index_Model extends Model
{
public function __construct()
{
parent::__construct();
}
public function getAllrecords()
{
return $this->db->select("SELECT * FROM `mvc` ORDER BY id DESC");
}
public function getOnerecord($id)
{
return $this->db->select("SELECT * FROM `mvc` WHERE id='".$id."' LIMIT 1");
}
public function edit_submit_index($data,$arg)
{
$this->db->update('mvc', $data,
"`id` = $arg");
}
}
Here is the view file index.php which inside views folder contains the update form
views/index/index.php
<?php
$name=$this->content[0]['name'];
$email=$this->content[0]['email'];
$contact=$this->content[0]['contact'];
?>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<form action="<?php echo URL; ?>index/edit_submit_index" method="post" enctype="multipart/form-data" onsubmit="return confirm('Do you really want to submit the form?');">
<div class="col-xs-6 form-group">
<label class="control-label col-xs-6" for="name">name:</label>
<div class="col-xs-6">
<input class="form-control" name="name" id="name" placeholder="Enter name" value="<?php echo $name; ?>">
</div>
</div>
<div class="col-xs-6 form-group">
<label class="control-label col-xs-6" for="email">email:</label>
<div class="col-xs-6">
<input type="text" class="form-control" name="email" id="email" placeholder="Enter email" value="<?php echo $email; ?>">
</div>
</div>
<div class="col-xs-6 form-group">
<label class="control-label col-xs-6" for="contact">contact:</label>
<div class="col-xs-6">
<input type="text" class="form-control" name="contact" id="contact" placeholder="Enter contact" value="<?php echo $contact; ?>">
</div>
</div>
<div class="col-xs-6 form-group">
<div class="col-sm-offset-2 col-xs-6">
<button type="submit" class=".btn-info form-control" value="update" name="submit">Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
Path: localhost/project_folder_name/view_folder_name/view_filename
Example: localhost/mvc/index/index