攻克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 (!emptyempty($this->data))
  17. {
  18. if ($this->Company->save($this->data))
  19. {
  20. $this->flash('Your post has been saved.','/companies');
  21. }
  22. }
  23. }
  24. function edit($id = null)
  25. {
  26. if (emptyempty($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('Your post has been updated.','/companies');
  36. }
  37. }
  38. }
  39. function delete($id)
  40. {
  41. $this->Company->del($id);
  42. $this->flash('The post with id: '.$id.' has been deleted.', '/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. <h1>Test companies</h1>
  2. <table>
  3. <tr>
  4. <th>Id</th>
  5. <th>company</th>
  6. <th>price</th>
  7. <th>change</th>
  8. <th>last update</th>
  9. </tr>
  10. <?php foreach ($companies as $company): ?>
  11. <tr>
  12. <td><?php echo $company['Company']['id']; ?></td>
  13. <td>
  14. <?php echo $html->link($company['Company']['company'], "/companies/view/".$company['Company']['id']); ?>
  15. <?php echo $html->link('Delete', "/companies/delete/{$company['Company']['id']}", null, 'Are you sure?')?>
  16. </td>
  17. <td><?php echo $company['Company']['price']; ?></td>
  18. <td><?php echo $company['Company']['change']; ?></td>
  19. <td><?php echo $company['Company']['lastupd']; ?></td>
  20. </tr>
  21. <?php endforeach; ?>
  22. </table>
  23. <p>
  24. <?php echo $html->link('add', "/companies/add"); ?>
  25. </p>

view.thtml:

  1. <h1>Company: <?php echo $company['Company']['company']?></h1>
  2. <p><small>Id: <?php echo $company['Company']['id']?></small></p>
  3. <p>Price: <?php echo $company['Company']['price']?></p>
  4. <p>Change: <?php echo $company['Company']['change']?></p>
  5. <p>LastUpdate: <?php echo $company['Company']['lastupd']?></p>
  6. <br/>
  7. <p>
  8. <?php echo $html->link('edit', "/companies/edit/".$company['Company']['id']); ?>
  9. </p>

add.thtml:

  1. <h1>Add Company</h1>
  2. <form method="post" action="<?php echo $html->url('/companies/add')?>">
  3. <p>
  4. Company:
  5. <?php echo $html->input('Company/company', array('size' => '40'))?>
  6. <?php echo $html->tagErrorMsg('Company/company', 'Company is required.') ?>
  7. </p>
  8. <p>
  9. Price:
  10. <?php echo $html->input('Company/price', array('size' => '40'))?>
  11. <?php echo $html->tagErrorMsg('Company/company', 'Price is required.') ?>
  12. </p>
  13. <p>
  14. Change:
  15. <?php echo $html->input('Company/change', array('size' => '40'))?>
  16. <?php echo $html->tagErrorMsg('Company/change', 'Change is required.') ?>
  17. </p>
  18. <p>
  19. Last Update:
  20. <?php echo $html->input('Company/lastupd', array('size' => '40'))?>
  21. <?php echo $html->tagErrorMsg('Company/lastupd', 'Last Update is required.') ?>
  22. </p>
  23. <p>
  24. <?php echo $html->submit('Save') ?> <?php echo $html->link('return', "/companies/index"); ?>
  25. </p>
  26. </form>

edit.thtml:

  1. <h1>Edit Company</h1>
  2. <form method="post" action="<?php echo $html->url('/companies/edit')?>">
  3. <?php echo $html->hidden('Company/id'); ?>
  4. <p>
  5. Company:
  6. <?php echo $html->input('Company/company', array('size' => '40'))?>
  7. <?php echo $html->tagErrorMsg('Company/company', 'Company is required.') ?>
  8. </p>
  9. <p>
  10. Price:
  11. <?php echo $html->input('Company/price', array('size' => '40'))?>
  12. <?php echo $html->tagErrorMsg('Company/company', 'Price is required.') ?>
  13. </p>
  14. <p>
  15. Change:
  16. <?php echo $html->input('Company/change', array('size' => '40'))?>
  17. <?php echo $html->tagErrorMsg('Company/change', 'Change is required.') ?>
  18. </p>
  19. <p>
  20. Last Update:
  21. <?php echo $html->input('Company/lastupd', array('size' => '40'))?>
  22. <?php echo $html->tagErrorMsg('Company/lastupd', 'Last Update is required.') ?>
  23. </p>
  24. <p>
  25. <?php echo $html->submit('Save') ?> <?php echo $html->link('return', "/companies/index"); ?>
  26. </p>
  27. </form>

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



相关阅读:
PHP无限分类代码,支持数组格式化、直接输出菜单两种方式
在VB.NET中使用MS Access存储过程
网页设计学习教程:CSS盒模型
利用缓冲技术提高JSP程序的性能和稳定性
DataList控件也玩分页
LinQ学习之旅 从整型数组中找出偶数
freebsd如何是启动使Booting [kernet] in 9 seconds ...变成0秒
SQL Server 2008的透明数据加密上
技巧:为文件夹添加背景音乐的操作
新手学堂:Linux操作系统telnet配置方法
一周让你学会PHP 不错的学习资料
强烈推荐 - Ajax 技术资源中心
获取JavaScript用户自定义类的类名称
PHP在特殊字符前加斜杠的实现代码
快速导航

Copyright © 2016 phpStudy | 皖ICP备18014864号-4