Zend Framework Database Step By Step Tutorial - Part 6: To create editing form, add a method in controller. In this practice, we add a action named "editAction". This action will show individual data that filtered by GET http parameter.
Open "UserController.php" within application/controllers. Add following method:
public function editAction()
{
$params = array('host' =>'localhost',
'username' =>'root',
'password' =>'admin',
'dbname' =>'zend'
);
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
$request = $this->getRequest();
$id = $request->getParam("id");
$sql = "SELECT * FROM `user` WHERE id='".$id."'";
$result = $DB->fetchRow($sql);
$this->view->assign('data',$result);
$this->view->assign('action', $request->getBaseURL()."/user/processedit");
$this->view->assign('title','Member Editing');
$this->view->assign('label_fname','First Name');
$this->view->assign('label_lname','Last Name');
$this->view->assign('label_uname','User Name');
$this->view->assign('label_pass','Password');
$this->view->assign('label_submit','Edit');
$this->view->assign('description','Please update this form completely:');
}
Then, create a file named "edit.phtml" within application/views/scripts/user. Enter following code:
<? include "header.phtml"; ?>
<h1><?=$this->escape($this->title);?></h1>
<div id="description">
<?=$this->escape($this->description);?>
</div>
<form name="edit" method="post" action="<?=$this->escape($this->action)?>">
<input type="hidden" name="id" value="<?=$this->data['id']?>">
<table>
<tr>
<td><?=$this->escape($this->label_fname)?></td>
<td><input type="text" name="first_name" value="<?=$this->data['first_name']?>"></td>
</tr>
<tr>
<td><?=$this->escape($this->label_lname)?></td>
<td><input type="text" name="last_name" value="<?=$this->data['last_name']?>"></td>
</tr>
<tr>
<td><?=$this->escape($this->label_uname)?></td>
<td><input type="text" name="user_name" value="<?=$this->data['user_name']?>"></td>
</tr>
</table>
<input type="submit" name="submit" value="<?=$this->escape($this->label_submit);?>">
</form>
<? include "footer.phtml"; ?>
Then open your table data again at http://localhost/test/zend/helloworld/web_root/user/list. Klik a edit link at one of rows. The page will jump to edit form such as:
