| « Zend Framework Database: Updating Data Use Update Query From... | Zend Framework Database: Delete Query Style » |
Zend Framework Database: Deleting Data
Zend Framework Database Step By Step Tutorial - Part 9: Last action from this series is delete data. For this job, create new method in controller.
Open your "UserController.php" within application/controllers. Add new method for delete action (i give name "delAction"):
public function delAction()
{
$params = array('host' =>'localhost',
'username' =>'root',
'password' =>'admin',
'dbname' =>'zend'
);
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
$request = $this->getRequest();
$sql = "DELETE FROM `user` WHERE id='".$request->getParam('id')."'";
$DB->query($sql);
$this->view->assign('title','Delete Data');
$this->view->assign('description','Deleting succes');
$this->view->assign('list',$request->getBaseURL()."/user/list");
Next, create a view for this action. Create a file named "del.phtml" within application/views/scripts/user. Enter following code:
<? include "header.phtml"; ?> <h1><?=$this->escape($this->title);?></h1> <h2><?=$this->escape($this->description);?></h2> <a href="<?=$this->escape($this->list);?>">Member List</a> <? include "footer.phtml"; ?>
Now, try to test from table data. Click link del at one of rows.

