Home PHP Framework ThinkPHP How to implement addition, deletion, modification and query in thinkphp3

How to implement addition, deletion, modification and query in thinkphp3

Apr 17, 2023 am 10:28 AM

This article will introduce you to the addition, deletion, modification and search operations under the ThinkPHP3 framework. I hope it will be helpful to developers who are learning the ThinkPHP3 framework.

  1. Add operation

In ThinkPHP3, you can use the M method to add a piece of data, for example:

$Model = M('User');
$data['name'] = 'wuzhi';
$data['password'] = md5('123456');
$result = $Model->add($data);
Copy after login

The above code means to instantiate the User model , and set the name and password attributes for it, and then insert the data into the database through the add method. $result is the primary key value returned after insertion.

  1. Query operation

To query a single data, use the find method, to query multiple data, use the select method, for example:

$Model = M('User');
$condition['id'] = 1;
$result = $Model->where($condition)->find();
Copy after login

The above code means query id User data of 1 and return the result.

$Model = M('User');
$condition['id'] = array('between',array(1,10));
$result = $Model->where($condition)->select();
Copy after login

The above code queries user data with IDs between 1-10 and returns the results.

  1. Modification operation

Use the save method to modify existing data, for example:

$Model = M('User');
$condition['id'] = 1;
$data['name'] = 'lisi';
$data['password'] = md5('654321');
$result = $Model->where($condition)->save($data);
Copy after login

The above code will change the name and The password is changed to lisi and 654321.

  1. Delete operation

Use the delete method to delete data, for example:

$Model = M('User');
$condition['id'] = 1;
$result = $Model->where($condition)->delete();
Copy after login

The above code deletes the user data with id 1.

The above is a basic introduction to the addition, deletion, modification and search operations under the ThinkPHP3 framework. In actual development, we need to flexibly use the above methods to achieve various functions. I hope this article can help everyone.

The above is the detailed content of How to implement addition, deletion, modification and query in thinkphp3. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)