Rumah > php教程 > PHP开发 > 攻克CakePHP系列三 表单数据增删改

攻克CakePHP系列三 表单数据增删改

黄舟
Lepaskan: 2016-12-19 10:53:20
asal
1206 orang telah melayarinya

下面的代码是Cakephp下对表单数据的增加,删除,修改实现代码

这里声明一点,上例中不小心把数据库表中lastupd字段错打成lastudp,本例子予以更正。

除上诉字段数据库与上例一致。

工程仍沿用上例,如下图:

 

代码依次为:

database.php:与上例一致。

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$validate=array(
'company'=> VALID_NOT_EMPTY,
'PRice'=> VALID_NOT_EMPTY,
'change'=> VALID_NOT_EMPTY,
'lastupd'=> VALID_NOT_EMPTY
);
}
?>
index.thtml:

Test companies



















Idcompanypricechangelast update

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

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



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


view.thtml:

Company:


Id:


Price:


Change:


LastUpdate:





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中文网(m.sbmmt.com)!


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan