Table of Contents
Test companies
Add Company
Edit Company
Home Backend Development PHP Tutorial 攻克CakePHP系列三 表单数据增删改_php技巧

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

May 17, 2016 am 09:34 AM
cakephp form data

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

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

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

代码依次为:

database.php:与上例一致。

companies_controller.php:

  1. class CompaniesController extends AppController
  2. {
  3.     var $name = 'Companies';
  4.     
  5.     function index()
  6.     {
  7.         $this->set('companies'$this->Company->findAll());
  8.     }
  9.     
  10.     function view($id = null)
  11.     {
  12.         $this->Company->id = $id;
  13.         $this->set('company'$this->Company->read());
  14.     }
  15.     
  16.     function add()
  17.     {
  18.         if (!emptyempty($this->data))
  19.         {
  20.             if ($this->Company->save($this->data))
  21.             {
  22.                 $this->flash('Your post has been saved.','/companies');
  23.             }
  24.         }
  25.     }
  26.     
  27.     function edit($id = null)
  28.     {
  29.         if (emptyempty($this->data))
  30.         {
  31.             $this->Company->id = $id;
  32.             $this->data = $this->Company->read();
  33.         }
  34.         else
  35.         {
  36.             if ($this->Company->save($this->data['Company']))
  37.             {
  38.                 $this->flash('Your post has been updated.','/companies');
  39.             }
  40.         }
  41.     }
  42.     
  43.     function delete($id)
  44.     {
  45.         $this->Company->del($id);
  46.         $this->flash('The post with id: '.$id.' has been deleted.''/companies');
  47.     }
  48. }
  49. ?>

company.php:

  1. class Company extends AppModel
  2. {
  3.     var $name = 'Company';
  4.     
  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. 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.     
  7. echo $html->link('Delete'"/companies/delete/{$company['Company']['id']}", null, 'Are you sure?')?>
  8. echo $company['Company']['price']; ?> echo $company['Company']['change']; ?> echo $company['Company']['lastupd']; ?>
  9. 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']['lastupd']?>


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

add.thtml:

  1. Add Company

  2. "post" action="url('/companies/add')?>">
  3. Company:
  4. echo $html->input('Company/company'array('size' => '40'))?>
  5. echo $html->tagErrorMsg('Company/company''Company is required.') ?>
  6. Price:
  7. echo $html->input('Company/price'array('size' => '40'))?>
  8. echo $html->tagErrorMsg('Company/company''Price is required.') ?>
  9. Change:
  10. echo $html->input('Company/change'array('size' => '40'))?>
  11. echo $html->tagErrorMsg('Company/change''Change is required.') ?>
  12. Last Update:
  13. echo $html->input('Company/lastupd'array('size' => '40'))?>
  14. echo $html->tagErrorMsg('Company/lastupd''Last Update is required.') ?>
  15. echo $html->submit('Save') ?> echo $html->link('return'"/companies/index"); ?>

edit.thtml:

  1. Edit Company

  2. "post" action="url('/companies/edit')?>">
  3. echo $html->hidden('Company/id'); ?>
  4. Company:
  5. echo $html->input('Company/company'array('size' => '40'))?>
  6. echo $html->tagErrorMsg('Company/company''Company is required.') ?>
  7. Price:
  8. echo $html->input('Company/price'array('size' => '40'))?>
  9. echo $html->tagErrorMsg('Company/company''Price is required.') ?>
  10. Change:
  11. echo $html->input('Company/change'array('size' => '40'))?>
  12. echo $html->tagErrorMsg('Company/change''Change is required.') ?>
  13. Last Update:
  14. echo $html->input('Company/lastupd'array('size' => '40'))?>
  15. echo $html->tagErrorMsg('Company/lastupd''Last Update is required.') ?>
  16. echo $html->submit('Save') ?> echo $html->link('return'"/companies/index"); ?>

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

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

How to use Twig with CakePHP? How to use Twig with CakePHP? Jun 05, 2023 pm 07:51 PM

Using Twig in CakePHP is a way to separate templates and views, making the code more modular and maintainable. This article will introduce how to use Twig in CakePHP. 1. Install Twig. First install the Twig library in the project. You can use Composer to complete this task. Run the following command in the console: composerrequire "twig/twig:^2.0" This command will be displayed in the project's vendor

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Steps to implement image uploading and display using CakePHP framework Steps to implement image uploading and display using CakePHP framework Jul 29, 2023 pm 04:21 PM

Steps to implement image upload and display using CakePHP framework Introduction: In modern web applications, image upload and display are common functional requirements. The CakePHP framework provides developers with powerful functions and convenient tools, making it simple and efficient to upload and display images. This article will introduce you to how to use the CakePHP framework to upload and display images. Step 1: Create a file upload form First, we need to create a form in the view file for users to upload images. The following is an example of

Java develops custom templates and style functions for form data Java develops custom templates and style functions for form data Aug 07, 2023 pm 02:49 PM

Java develops custom templates and style functions for form data. With the development of the Internet, form data is used more and more widely in web pages. For developers, how to implement customized templates and style functions for form data is a common requirement. This article will introduce how to use Java development to implement this function, and provide code examples for readers' reference. 1. Requirements Analysis During the development process, we often encounter situations where we need to use different templates and styles to display form data. For example, when a company publishes job information on a recruitment website,

See all articles