Home  >  Article  >  php教程  >  Overcoming CakePHP Series 3: Adding, Deleting and Modifying Form Data

Overcoming CakePHP Series 3: Adding, Deleting and Modifying Form Data

黄舟
黄舟Original
2016-12-19 10:53:201174browse

The following code is the implementation code for adding, deleting, and modifying form data under Cakephp.

I would like to make a statement here. In the above example, the lastupd field in the database table was accidentally typed as lastudp. This example will be corrected.

Except the appeal field database is the same as the above example.

The project still uses the above example, as shown below:

The code is:

database.php: consistent with the above example.

companies_controller.php:

classCompaniesControllerextendsAppController
{
var$name='Companies';

functionindex()
{
$this->set('companies',$this->Company ->findAll());
}

functionview($id= null)
{
$this->Company->id =$id;
$this->set('company',$this ->Company->read());
}

functionadd()
{
if(!emptyempty($this->data))
{
if($this->Company->save ($this->data))
{
$this->Flash('Your post has been saved.','/companies');
}
}
}

functionedit($id= null)
{
if(emptyempty($this->data))
{
$this->Company->id =$id;
$this->data =$this->Company->read( );
}
else
{
if($this->Company->save($this->data['Company']))
{
$this->flash('Your post has been updated.','/companies');
}
}
}

functiondelete($id)
{
$this->Company->del($id);
$this->flash(' The post with id: '.$id.' has been deleted.','/companies');
}
}
?>
company.php:

classCompanyextendsAppModel
{
var$name = 'Company';

var $ value = array (
'Company '= & GT; Valid_not_empty,
'Price' = & GT; Valid_Not_empty,
'Change '= & GT; Vali D_not_empty,
'Lastupd '= & GT; Valid_not_empty
) ;
}
?>
index.thtml:

Test companies

















Idcompany< ;/th>
pricechangelast update

link($company['Company']['company'],"/companies/view/".$company['Company']['id']); ?>

< ?phpecho$html->link('Delete',"/companies/delete/{$company['Company']['id']}", null,'Are you sure?')?>
< /td>



link('add',"/companies/add"); ?>

view.thtml:

Company:


Id:


Price:


Change:


LastUpdate: < ?phpecho$company['Company']['lastupd']?>





link('edit', "/companies/edit/".$company['Company']['id']); ?>


add.thtml:

Add Company




Company:
input('Company/company',array('size'=>'40'))?>
tagErrorMsg('Company/company','Company is required.') ?>



Price:
input('Company/price',array('size'=>'40'))?>
tagErrorMsg('Company/company','Price is required.') ?>



Change:
input('Company/change',array('size'=>'40'))?>
tagErrorMsg('Company/change','Change is required.') ?>



Last Update:
input('Company/lastupd',array('size'=>'40'))?>
tagErrorMsg('Company/lastupd','Last Update is required.') ?>



submit('Save') ?> link('return',"/companies/index"); ?>



edit.thtml:

Edit Company



hidden('Company/id'); ?>


Company:
input('Company/company',array('size'=>'40'))?>
tagErrorMsg('Company/company','Company is required.') ?>



Price:
input('Company/price',array('size'=>'40'))?>
tagErrorMsg('Company/company','Price is required.') ?>



Change:
input('Company/change',array('size'=>'40'))?>
tagErrorMsg('Company/change','Change is required.') ?>



Last Update:
input('Company/lastupd',array('size'=>'40'))?>
tagErrorMsg('Company/lastupd','Last Update is required.') ?>



submit('Save') ?> link('return',"/companies/index"); ?>



如此访问http://localhost/cakephp/companies即可测试代码。

 以上就是攻克CakePHP系列三 表单数据增删改的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn