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

前端之家收集整理的这篇文章主要介绍了攻克CakePHP系列三 表单数据增删改前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

代码依次为:

database.PHP:与上例一致。

companies_controller.PHP

  1. PHP
  2. class

    CompaniesController

    extends

    AppController
  3. {
  4. var

    $name='Companies';
  5. function

    index()
  6. {
  7. $this->set('companies',$this->Company->findAll());
  8. }
  9. function

    view($id=null)
  10. {
  11. $this->Company->id=$id;
  12. $this->set('company',$this->Company->read());
  13. }
  14. function

    add()
  15. {
  16. if

    (!empty

    empty

    ($this->data))
  17. {
  18. if

    ($this->Company->save($this->data))
  19. {
  20. $this->flash('Yourposthasbeensaved.','/companies');
  21. }
  22. }
  23. }
  24. function

    edit($id=null)
  25. {
  26. if

    (empty

    empty

    ($this->data))
  27. {
  28. $this->Company->id=$id;
  29. $this->data=$this->Company->read();
  30. }
  31. else

  32. {
  33. if

    ($this->Company->save($this->data['Company']))
  34. {
  35. $this->flash('Yourposthasbeenupdated.','/companies');
  36. }
  37. }
  38. }
  39. function

    delete($id)
  40. {
  41. $this->Company->del($id);
  42. $this->flash('Thepostwithid:'.$id.'hasbeendeleted.','/companies');
  43. }
  44. }
  45. ?>

company.PHP

  1. PHP
  2. class

    Company

    extends

    AppModel
  3. {
  4. var

    $name='Company';
  5. var

    $validate=

    array

    (
  6. 'company'=>VALID_NOT_EMPTY,
  7. 'price'=>VALID_NOT_EMPTY,
  8. 'change'=>VALID_NOT_EMPTY,
  9. 'lastupd'=>VALID_NOT_EMPTY
  10. );
  11. }
  12. ?>

index.thtml:

  1. Testcompanies

  2. Id
  3. company
  4. price
  5. change
  6. lastupdate
  7. PHP

    foreach

    ($companies

    as

    $company):?>
  8. PHPecho$company['Company']['id'];?>
  9. PHPecho$html->link($company['Company']['company'],"/companies/view/".$company['Company']['id']);?>
  10. PHPecho$html->link('Delete',"/companies/delete/{$company['Company']['id']}",null,'Areyousure?')?>
  11. PHPecho$company['Company']['price'];?>
  12. PHPecho$company['Company']['change'];?>
  13. PHPecho$company['Company']['lastupd'];?>
  14. PHP

    endforeach

    ;?>
  15. PHPecho$html->link('add',"/companies/add");?>

view.thtml:

  1. Company:PHPecho$company['Company']['company']?>

  2. Id:PHPecho$company['Company']['id']?>

  3. Price:PHPecho$company['Company']['price']?>

  4. Change:PHPecho$company['Company']['change']?>

  5. LastUpdate:PHPecho$company['Company']['lastupd']?>


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

add.thtml:

  1. AddCompany

  2. "post"action="url('/companies/add')?>">
  3. Company:
  4. PHPecho$html->input('Company/company',

    array

    ('size'=>'40'))?>
  5. PHPecho$html->tagErrorMsg('Company/company','Companyisrequired.')?>
  6. Price:
  7. PHPecho$html->input('Company/price',

    array

    ('size'=>'40'))?>
  8. PHPecho$html->tagErrorMsg('Company/company','Priceisrequired.')?>
  9. Change:
  10. PHPecho$html->input('Company/change',

    array

    ('size'=>'40'))?>
  11. PHPecho$html->tagErrorMsg('Company/change','Changeisrequired.')?>
  12. LastUpdate:
  13. PHPecho$html->input('Company/lastupd',

    array

    ('size'=>'40'))?>
  14. PHPecho$html->tagErrorMsg('Company/lastupd','LastUpdateisrequired.')?>
  15. PHPecho$html->submit('Save')?>PHPecho$html->link('return',"/companies/index");?>

edit.thtml:

  1. EditCompany

  2. "post"action="url('/companies/edit')?>">
  3. PHPecho$html->hidden('Company/id');?>
  4. Company:
  5. PHPecho$html->input('Company/company','Companyisrequired.')?>
  6. Price:
  7. PHPecho$html->input('Company/price','Priceisrequired.')?>
  8. Change:
  9. PHPecho$html->input('Company/change',

    array

    ('size'=>'40'))?>
  10. PHPecho$html->tagErrorMsg('Company/change','Changeisrequired.')?>
  11. LastUpdate:
  12. PHPecho$html->input('Company/lastupd',

    array

    ('size'=>'40'))?>
  13. PHPecho$html->tagErrorMsg('Company/lastupd','LastUpdateisrequired.')?>
  14. PHPecho$html->submit('Save')?>PHPecho$html->link('return',"/companies/index");?>

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

原文链接:https://www.f2er.com/php/29415.html

猜你在找的PHP相关文章