Thursday, November 12, 2009

CakePHP - Edit a Record in Database

So, the keen readers may guess what we need to do to edit a record in our 'posts' table.
Step:
1. Open posts_controller.php (found under '/app/controllers' folder).
2. Copy-paster the function edit() to edit record.


function edit($id = null) 
{   $this->Post->id = $id; 
if (empty($this->data)) { 
$this->data = $this->Post->read(); 
}
else {  
if ($this->Post->save($this->data)) {

  $this->Session->setFlash('Your post has been updated.');
  $this->redirect(array('action' => 'index')); 
}  
}  


}
As such, this function edit() is self-explanatory. If you read it carefully, the function edit() expects '$id' value.
$this=>data contains form submitted data. If it is empty ($this=>data will obviously be empty the first time you try to edit a record), the function $this->Post->read() reads the record from database and stores it at $this->data variable to display the same via edit form. If some data has been submitted, the function $this->Post->save($this->data); tries to save that data by updating the respective record.

Now we need to create a form (Cake calls it 'view') to edit record.
Step:
1. Create a new file.
2. Copy-paste following code:
<h1>Edit Post</h1>
<?php  
echo $form->create('Post', array('action' => 'edit'));  
echo $form->input('title');  
echo $form->input('body', array('rows' => '3'));  
echo $form->input('id', array('type'=>'hidden'));  
echo $form->end('Save Post');  
?>
3. Save this file as 'app/views/posts/edit.ctp'

Now we need to add a link to our index.ctp file to display edit option.

Step:
1. Open index.ctp (found under '/app/views/posts' folder)
2. Add the following line just below the line we have added to show 'delete' option.
<?php echo $html->link('Edit', array('action'=>'edit', 'id'=>$post['Post']['id']));?>
3. Save that file.

Now point your browser to:
http://caketest.local/posts

You can see the 'Edit' option.
Now if you follow 'Edit' link, you can see the form with respective record.
Here is a screenshot:


  
In my next blog post, I'll show you how to make your url SEO friendly using CakePHP.

12 comments:

Soul child media said...

hi,i tried this example to update a record in my database but all i get is a new record.

Siddhartha said...

This usually occurs when you have no ID value. In that case, Cake inserts a new record instead of updating it.

shajin kurumpanai said...

Dear siddhartha ,you code is really well,I have learn quickly(within 2 days) cakephp through your code.Thanks

Siddhartha said...

Thanks Shajin,

I am really glad to know that i could help you.

Best,
Sid

ARUN said...

Great work dude..Really helpful..
Thanks..

Siddhartha said...

Thanks Arun for your visit and comments.

Chris Wayne G. Comendador said...

@Soul: i think you missed to have the id field auto incremented!..

And thanks Siddhartha, this blog has been a helpful source for my studies..

Thanks!

Siddhartha said...

Thanks Chris once again for your visit and comments, i hope it will help others as well. :)

Benedict Bisana said...

Hi Siddhartha,

How am I going to edit, for example my profile, without passing my id on the browser using get method?

Like when I click "Edit Profile" the Form automatically get the info from the database.

Siddhartha said...

I think Cake saves your login details in $session variable. So, you always have access to such details once you login.

justAWord said...

can we use another attribute instead of id. ?

Unknown said...

it's only add data to new record