2013-12-21 55 views
0

我是新来的Zend框架,我试图更新数据库和网格中的数据,而不是更新特定的行所有的行都得到更新。请帮我解决一下这个。更新查询更新数据库中的所有行而不是特定行

这是我的控制器代码。

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

这是我的型号代码。

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

http://stackoverflow.com/questions/4097234/how-update-a-database-table-record-in-zend –

回答

0

尝试改变你的代码,所以它看起来是这样的:

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

感谢您的回复andylibrian。我将我的ID改为$ id,但它给出了一个错误“未定义的变量”。 – Tuhin

+0

您是否添加了以下行? '$ ID = $这 - > Request()方法 - > getparam( 'ID'); //定义你的id'该代码定义了你的id。 –

+0

对不起,我没有定义ID,现在它工作正常。非常感谢您的帮助。 – Tuhin

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

你传递的文本字符串'Id'$id参数updateClient。在updateClient内部,您使用"Id=$id"建立了一个条件。您的条件成为where Id=Id,对于每条记录都是如此,因此每条记录都会更新。

您需要传递一个包含要更新记录的实际ID的变量。