سؤال

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");
}
هل كانت مفيدة؟

المحلول 2

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');
}

نصائح أخرى

  $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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top