I am new to zend framework and want to delete data from database, but delete function is not working. Please help me out.
Here bis my controller code.
public function deleteAction()
{
if($this->getRequest()->isPost())
{
$del= $this->getRequest()->getPost('id');
if($del=="Yes")
{
$id =$this->getRequest()->getpost('id');
$client = new Application_Model_DbTable_Client();
$id = $this->getrequest()->getparam('id');
$client->deleteClient($id);
}
$this->_helper->redirector('index');
}
else
{
$id = $this->getRequest()->getparam('id');
$client = new Application_Model_DbTable_Client();
$this->view->client = $client->getclient($id);
}
}
Here is my model code.
public function deleteClient($id)
{
$this->delete('Id='.(int)$id);
}
Here is my delete.phtml file.
<form>
<p>Are you sure that you want to delete
<?php foreach($this->client as $clients): ?>
'<?php echo $this->escape($clients['firstname']); ?>'
'<?php echo $this->escape($clients['lastname']); ?>'
'<?php echo $this->escape($clients['email']); ?>'
</p>
<?php endforeach; ?>
<form action="<?php echo $this->url(array('action'=>'delete')); ?>" method="post">
<div>
<input type="hidden" name="id" value="<?php echo $this->escape($clients["Id"]); ?>"/>
<input type="submit" name="del" value="Yes" />
<input type="submit" name="del" value="No" />
</div>
</form>
i see where the problem is,
you are using ,
$this->getRequest()->getPost('id');
for both $id
AND $del
so if you are getting $id=1 as you are saying then $del="yes" will not be true!
so try to pass different id for $del in your view script or for the testing sack just remove that condition temporally..
try to pass the yes no buttons like this,
<input type="submit" class="btn-glow primary" name="del" value="Yes" />
<input type="submit" class="btn" name="del" value="No" />
then use
$del = $this->getRequest()->getPost('del');
update *
<input type="hidden" name="id" value="<?php echo $this->client['id'];?>"/>
Hope this Helps..
Please try below code :
public function deleteClient($id)
{
$this->dbAdapter->delete('table_name','Id='.(int)$id);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.