the edit function creates a new user and does not update the current user in the database

advertisements

I'm currently using CAKEPHP 2.3 and I'm trying to edit the user's information. but when I submit the information, it does not update the information in to the database. it instead creates a new user with the new information i inserted. i want it to update the user, not create a new one.

My code for the edit.ctp is:

    <h1>Edit Account information</h1>
<?php
echo $this->Form->create('User', array('action' => 'edit'));
echo $this->Form->input('username', array('value' => $this->Session->read('Auth.User.username')));
echo $this->Form->input('name', array('value' => $this->Session->read('Auth.User.name')));
echo $this->Form->end('Submit');
?>

And then my edit function in the users controller is:

public function edit() {
//        debug($this->User->save($this->request->data));
        $this->User->id = $this->Session->read('Auth.User.id');
        $this->request->data['User']['id'];
        if ($this->request->is('post')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The User has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
            }
        } else {
            $this->request->data = $this->User->read(null);
            isset($this->request->data['User']['password']);
        }
    }

Here is the index to get to the edit user page.

<h1>Users Home</h1>
<p>Welcome <?php print $this->Session->read('Auth.User.name');?> <br/></p>
<table border="0" width="200" text-align="center">
<tr>
<td width="50"><?php echo $this->Html->link('Log out', array('action' => 'logout')); ?></td>
<td width="50"><?php echo $this->Html->link('Edit', array('action' => 'edit')); ?></td>
<td width="50"><?php echo $this->Html->link('Add User', array('action' => 'add')); ?></td><!-- should only show for admin -->
<td width="50"><?php echo $this->html->link('Manage Users', array('action' => 'usermanage')); ?></td><!-- should only show for admin -->
</tr>
</table>

Many Thanks.


lose the $id in your edit method. usually a user can only - and only himself - edit his own record.

public function edit() {
    if ($this->request->is('post')) {
        $this->request->data['User']['id'] = $this->Session->read('Auth.User.id');
        if ($this->User->save($this->request->data)) {
        ...

and value is wrong for your forms. never use that. it breaks your form on post and validation errors (see http://www.dereuromark.de/2010/06/23/working-with-forms/ for the "why" - you will also find how you can set default values if you really need to).

since you pass down data via $this->request->data you need to leave them as they were:

echo $this->Form->input('username');

also lose the action. the form will automatically post to itself!

last but not least dont use read(), use find(first)