PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 18, 2022

[FIXED] on editing inserting a new row instead of updating the current row in cakephp 2x

 January 18, 2022     cakephp, edit     No comments   

Issue

I m having trouble in updating the student profile form,after i gave the user_id to the student profile.Every time when i try to edit the form, on saving it provide new id to the existing user. I don't know where I'm getting wrong. Plz help.Thanks in Advance. Here is my add and edit function in Student Profiles controller:

 public function add() {
   if ($this->request->is('post')) {
      $this->StudentProfile->create();
    $data = $this->request->data;
     $data['StudentProfile']['user_id'] = $this->Auth->user('id');
     // code implemented below               
     //$this->loadModel('User');

      if ($this->StudentProfile->save($data)) {
        //Update user here if Profile saved successfully 
        //$this->StudentProfile->id = $this->Auth->user('id');

            $this->Session->setFlash(__('Your account profile has been   created'));
            $this->redirect(array('controller' => 'studentprofiles',   'action' => 'index'));
        }else{
             $this->Session->setFlash(__('Your Profile was saved, but an  error has occurred while updating the Users table'));
            //Email your self the user ID here or something ??
        }
    } 
     $h=array(

  'fields' => array('Height.height'),
  'recrusive' => 0
);

    $this->loadModel('Height');
    $height = $this->Height->find('list',$h);
    $this->set(compact('height'));
}

public function edit($id = null) {
      if (!$this->StudentProfile->exists($id)) {
        throw new NotFoundException(__('Invalid student profile'));
      }
     if ($this->request->is(array('post', 'put'))) {
        $this->request->data['StudentProfile']['user_id'] = $this->Auth- >user('id');
      // code implemented below               
     // $this->loadModel('User');
        if ($this->StudentProfile->save($this->request->data)) {
            //$this->StudentProfile->id = $this->Auth->user('id');
            $this->Session->setFlash(__('The student profile has been   saved.'), 'default', array('class' => 'alert alert-success'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The student profile could not be   saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
        }
    } else {
        $options = array('conditions' => array('StudentProfile.' . $this-  >StudentProfile->primaryKey => $id));
        $this->request->data = $this->StudentProfile->find('first',   $options);
    }
      $h=array(

  'fields' => array('Height.height'),
  'recrusive' => 0
);

    $this->loadModel('Height');
    $height = $this->Height->find('list',$h);
    $this->set(compact('height'));
 }

Solution

Can't believe I'm replying to my own question . Well In case anyone have the same above problem,the solution is just add the following code after this-

 $this->request->data['StudentProfile']['user_id'] = $this->Auth->user('id');
 // add this code to avoiding giving new id to existing user on edit:
        $this->request->data['StudentProfile']['id'] = $id;


Answered By - skye
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing