tags:

views:

15

answers:

1

hi, this is the code for edit

function edit($id){

    if(!empty($this->data)) {
        if($this->Article->save($this->data)) {
            pr($this->Article);
            $this->Session->setFlash("Article Saved!");
            $this->redirect('/articles/view');        
            }    
    }
    else    
        $this->data = $this->Article->findByArticleId($id);

}

and for the view

<?php 
echo $form->create('Article', array('type' => 'post','action'=>'edit')); 
?>

<table>
<tr><td>Edit Article</td></tr>
<tr><td>Title: </td><td><?php echo $form->text('article_title',array('label'=>false,'type'=>'text','size'=>50)); ?></td></tr>
<tr><td>Text: </td><td><?php echo $form->textarea('article_text',array('label'=>false,'class'=>'ckeditor','columns'=>100,'row'=>8,'width'=>100));?></td></tr>
<tr><td>Published: </td><td><?php echo $form->radio('article_published',array('1'=>'Yes','0'=>'No'),array('legend'=>false));?></td></tr>
</table>
<?php echo $form->end('Save'); ?>

the problem is it always adds a new record instead of updating the current record. how can I fix it?

+3  A: 

The reason why a new record is inserted is that CakePHP method doesn't know the ID of the record you're editing. Basically, when data passed to the save() method does not contain the record ID or the ID does not exist in the table, save() will insert a new record instead of updating an existing one.

The usual way of fixing this would be adding echo $form->input('id'); to the form. This will add a hidden field with the ID of the record being edited to the form. At this point enabling the security component would be a good idea as it will help you prevent tampering with form data.

Mike