0

I am new to Zend framework and I am trying to update data in database and grid, but instead of updating particular row all rows are getting updated. please help me with this.

Here is my controller code.

public function editAction()
{

$form = new Application_Form_user();
$this->view->form = $form;

if($this->getRequest()->isPost())
{

$formData= $this->getRequest()->getPost();

if($form->isvalid($formData))
{
    $client= new Application_Model_DbTable_Client();
    $firstname = $formData['firstname'];
    $lastname = $formData['lastname'];
    $email = $formData['email'];

    $client->updateClient('Id',$firstname,$lastname,$email);

    $this->_helper->redirector('index');
}
else 
{
    $form->populate($formData);

}
}
else
{

    $id=$this->getRequest()->getparam('id');
    if($id>0)
    {

        $client= new Application_Model_DbTable_Client();
        $clients = $client->getClient('Id');

        $form->populate($clients[0]);
    }
}
}

And here is my model code.

public function updateClient($id,$firstname,$lastname,$email)
{
    $data=array('firstname'=>$firstname,
              'lastname'=>$lastname,
               'email'=>$email);
 $this->update($data,"Id=$id");
}
1

2 Answers 2

1
  $client->updateClient('Id',$firstname,$lastname,$email)

You're passing the literal string 'Id' as the $id parameter to updateClient. Inside updateClient, you build a where condition with "Id=$id". Your where condition becomes where Id=Id, which is true for every record, so every record is updated.

You need to pass a variable containing the actual ID of the record you want to update.

Sign up to request clarification or add additional context in comments.

Comments

0

try to change your code, so it looks like this:

if($form->isvalid($formData))
{
    $client= new Application_Model_DbTable_Client();
    $firstname = $formData['firstname'];
    $lastname = $formData['lastname'];
    $email = $formData['email'];

    $id=$this->getRequest()->getparam('id'); // define your id

    $client->updateClient($id,$firstname,$lastname,$email); // here, change Id into proper $id

    $this->_helper->redirector('index');
}

4 Comments

Thanks for your reply andylibrian. I changed my Id into $id but it gives an error "undefined variable".
Have you added following line? $id=$this->getRequest()->getparam('id'); // define your id That code defines your id.
Sorry I didn't had defined id, now it's working properly. Thanks a lot for helping me with this.
You're welcome. If your problem has been resolved, you may want to accept my answer so others can see that this question has been answered. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.