.


Divya


Update record CakePHP framework PHP


In this example we will discuss about how to update a record or data from MySQL database using CakePHP framework PHP.

To update the data in mysql table UPDATE statement is used.

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value 

Note: The WHERE clause specifies which data that should be updated. If you omit the WHERE clause, all records or data will be updated!


We use 4 file for update students data.

  1. routes.php (config/routes.php)
  2. UsersController.php (src/controller/UsersController.php)
  3. delete.ctp (src/Template/Users/delete.ctp)
  4. index.php (src/controller/index.php)

config/routes.php ( Controller file for Update)

<?php
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
Router::defaultRouteClass('DashedRoute'); Router::scope('/', function (RouteBuilder $routes) { $routes->connect('/users/edit', ['controller' => 'Users', 'action' => 'edit']); $routes->fallbacks('DashedRoute'); }); Plugin::routes();

src/controller/UsersController.php

<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\ORM\TableRegistry;
use Cake\Datasource\ConnectionManager;
class UsersController extends AppController{
public function index(){
$users = TableRegistry::get('users');
$query = $users->find();
$this->set('results',$query);
}
public function edit($id){
if($this->request->is('post')){
$firstname = $this->request->data('first_name');
$lastname= $this->request->data('last_name');
$city= $this->request->data('city');
$email= $this->request->data('email');
$users_table = TableRegistry::get('users');
$users = $users_table->get($id);
$users->first_name = $firstname;
$users->last_name = $lastname;
$users->city = $city;
$users->email= $email;
if($users_table->save($users))
echo "User is udpated";
$this->setAction('index');
} else {
$users_table = TableRegistry::get('users')->find();
$users = $users_table->where(['id'=>$id])->first();
$this->set('first_name',$users->first_name);
$this->set('last_name',$users->last_name);
$this->set('city',$users->city);
$this->set('email',$users->email);
$this->set('id',$id);
}
}
} ?>

src/Template/Users/index.ctp

<!DOCTYPE html>
<html>
<body>
<table border="1">
<thead>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>City name</th>
<th>Email</th>
<th>Action</th>
</thead>
<tbody>
<?php
foreach($this->a->fetchtable() as $row)
{
?>
<tr>
<td><?php echo $row->id ; ?></td>
<td><?php echo $row->first_name; ?></td>
<td><?php echo $row->last_name; ?></td>
<td><?php echo $row->city_name; ?></td>
<td><?php
$id=$row->id;
echo $row->email; ?></td>
echo "<td><a href = '".$this->Url->build (["controller" => "Users","action"=>"edit",$row->id])."'>Edit</a></td>";
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>

src/Template/Users/edit.ctp

<?php
echo $this->Form->create("Users",array('url'=>'/users/edit/'.$id));
echo $this->Form->input('first_name',['value'=>$first_name]);
echo $this->Form->input('last_name',['value'=>$last_name]);
echo $this->Form->input('city',['value'=>$city]);
echo $this->Form->input('email',['value'=>$email]);
echo $this->Form->button('Submit');
echo $this->Form->end();
?>

After fetch data the table look like this.

Id first name last name City name Email Id Action
1 Divyasundar Sahu Mumbai divyasundar@gmail.com Update
2 Hritika Sahu Pune hritika@gmail.com Update
3 Milan Jena Chennai milanjena@gmail.com Update

Now i am going to update the id 3 data. After change the data of id 3 the table look like this.

Id first name last name City name Email Id Action
1 Divyasundar Sahu Mumbai divyasundar@gmail.com Update
2 Hritika Sahu Pune hritika@gmail.com Update
3 Milan Kumar Jena Delhi milanjena@gmail.com Update


.