Overcome the problems of adding, deleting, and modifying CakePHP series three-form data

Source: Internet
Author: User
The following code adds, deletes, and modifies form data in CakePHP. here we declare a point. in the previous example, we accidentally typed the lastupd field in the database table into lastudp. In this example, we will correct it.

Except for the appeal field, the database is consistent with the previous example.

The project still follows the example above, for example:

The code is as follows:

Database. php: same as the previous example.

Companies_controller.php:

  1. ClassCompaniesControllerExtendsAppController
  2. {
  3. Var $ Name='Companys';
  4. FunctionIndex ()
  5. {
  6. $ This-> Set ('Companys',$ This-> Company-> findAll ());
  7. }
  8. FunctionView ($ Id= Null)
  9. {
  10. $ This-> Company-> id =$ Id;
  11. $ This-> Set ('Company',$ This-> Company-> read ());
  12. }
  13. FunctionAdd ()
  14. {
  15. If(!EmptyEmpty($ This-> Data ))
  16. {
  17. If($ This-> Company-> save ($ This-> Data ))
  18. {
  19. $ This-> Flash ('Your post has been saved .','/Companies');
  20. }
  21. }
  22. }
  23. FunctionEdit ($ Id= Null)
  24. {
  25. If(EmptyEmpty($ This-> Data ))
  26. {
  27. $ This-> Company-> id =$ Id;
  28. $ This-> Data =$ This-> Company-> read ();
  29. }
  30. Else
  31. {
  32. If($ This-> Company-> save ($ This-> Data ['Company'])
  33. {
  34. $ This-> Flash ('Your post has been updated .','/Companies');
  35. }
  36. }
  37. }
  38. Function Delete($ Id)
  39. {
  40. $ This-> Company-> del ($ Id);
  41. $ This-> Flash ('The post with id :'.$ Id.'Has been deleted .','/Companies');
  42. }
  43. }
  44. ?>

Company. php:

  1. ClassCompanyExtendsAppModel
  2. {
  3. Var $ Name='Company';
  4. Var $ Validate=Array(
  5. 'Company'=> VALID_NOT_EMPTY,
  6. 'Price'=> VALID_NOT_EMPTY,
  7. 'Change'=> VALID_NOT_EMPTY,
  8. 'Lastup'=> VALID_NOT_EMPTY
  9. );
  10. }
  11. ?>

Index. thtml:

  1. Test companies
  2. Foreach($ Companies As $ Company):?>
  3. Endforeach;?>
  4. Id Company Price Change Last update
    Echo $ Company['Company'] ['Id'];?>
  5. Echo $ Html-> Link ($ Company['Company'] ['Company'],"/Companies/view /".$ Company['Company'] ['Id']);?>
  6. Echo $ Html-> Link ('Delete',"/Companies/delete/{$ company ['Company'] ['id']}", Null,'Are you sure? ')?>
  7. Echo $ Company['Company'] ['Price'];?> Echo $ Company['Company'] ['Change'];?> Echo $ Company['Company'] ['Lastup'];?>
  8. Echo $ Html-> Link ('Add',"/Companies/add");?>

View. thtml:

  1. Company: Echo $ Company['Company'] ['Company']?>
  2. Id: Echo $ Company['Company'] ['Id']?>

  3. Price: Echo $ Company['Company'] ['Price']?>

  4. Change: Echo $ Company['Company'] ['Change']?>

  5. LastUpdate: Echo $ Company['Company'] ['Lastup']?>


  6. Echo $ Html-> Link ('Edit',"/Companies/edit /".$ Company['Company'] ['Id']);?>

Add. thtml:

  1. Add Company

Edit. thtml:

  1. Edit Company

Access http: // localhost/cakephp/companies to test the code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.