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>
2
  • what error are you getting ?? please comment the redirect part and try to echo the value of $id,. Commented Dec 23, 2013 at 8:10
  • Thanks for replying Ronak I tried it and it's giving the value of id which is 1. Commented Dec 23, 2013 at 8:35

2 Answers 2

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

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

7 Comments

please check also the delete.phtml if i've committed any error there.
why are you using foreach in view script you can directly use $this->client, see update
without for each loop it's giving error, actually I am displaying data in grid from database.
it's giving error of undefined index firstname, lastname, email
okay dont touch the foreach , just add the hidden button line that i wrote in dont use $clients['id'], use $this->client['id'];
|
0

Please try below code :

public function deleteClient($id)
{
  $this->dbAdapter->delete('table_name','Id='.(int)$id);
}

Comments

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.