0

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>
Tuhin
  • 103
  • 1
  • 2
  • 12

2 Answers2

1

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

Ronak K
  • 1,567
  • 13
  • 23
  • please check also the delete.phtml if i've committed any error there. – Tuhin Dec 23 '13 at 09:44
  • why are you using `foreach` in view script you can directly use $this->client, see update – Ronak K Dec 23 '13 at 09:50
  • without for each loop it's giving error, actually I am displaying data in grid from database. – Tuhin Dec 23 '13 at 10:00
  • it's giving error of undefined index firstname, lastname, email – Tuhin Dec 23 '13 at 10:00
  • okay dont touch the foreach , just add the hidden button line that i wrote in dont use $clients['id'], use $this->client['id']; – Ronak K Dec 23 '13 at 10:03
  • I did that but nothing is happening and url is showing this [http://project.com/client/delete/id/8?id=%0D%0ANotice%3A+Undefined+index%3A+Id+in+%2Fvar%2Fwww%2Fdocs%2FProject.com%2Fpublic%2Fapplication%2Fviews%2Fscripts%2Fclient%2Fdelete.phtml+on+line+18%0D%0A&del=Yes] – Tuhin Dec 23 '13 at 10:10
  • When I am using $clients["Id"] url is showing something like this [http://project.com/client/delete/id/1?id=1&del=Yes] i think there is something wrong in the url part in delete.phtml – Tuhin Dec 24 '13 at 05:27
0

Please try below code :

public function deleteClient($id)
{
  $this->dbAdapter->delete('table_name','Id='.(int)$id);
}
Kevin G Flynn
  • 231
  • 4
  • 14