Home > PHP Framework > ThinkPHP > body text

How to perform addition, deletion and modification operations under the ThinkPHP framework

PHPz
Release: 2023-04-10 09:33:34
Original
518 people have browsed it

ThinkPHP is an open source PHP development framework. Its core concept is to simplify development and improve efficiency. In actual development, addition, deletion and modification are the basic operations of every web application. This article will introduce how to perform addition, deletion and modification operations under the ThinkPHP framework.

1. Add a record

To add a new record in ThinkPHP, you need to use a model and a controller. First, you need to define the table name and field information in the model. For example, to add a record to a student table, you can first define the table name and field information in the model:

class StudentModel extends Model
{
    protected $tableName = 'student';    //表名
    protected $fields = array('id', 'name', 'age', 'sex');    //字段信息
}
Copy after login

Then, create a Student object in the controller and specify the data to be added:

public function add()
{
    $student = D('Student');    //实例化Student对象
    $data = array(
        'name' => 'Tom',
        'age' => 18,
        'sex' => '男'
    );    //要添加的数据
    $student->add($data);    //添加数据
}
Copy after login

2. Delete records

To delete a record in ThinkPHP, you need to use a model and a controller. First, create a Student object in the controller and delete it based on the ID of the record to be deleted:

public function delete()
{
    $id = 1;    //要删除的记录的ID
    $student = D('Student');    //实例化Student对象
    $student->delete($id);    //执行删除操作
}
Copy after login

ThinkPHP's delete method will automatically delete data based on the primary key. If you need to delete records based on other conditions, you can An array is passed in as the second parameter in the delete method, for example:

public function delete()
{
    $condition = array('age' => array('gt', 18));    //删除满足条件的记录(年龄大于18岁的记录)
    $student = D('Student');    //实例化Student对象
    $student->where($condition)->delete();    //执行删除操作
}
Copy after login

3. Modify the record

To modify a record in ThinkPHP, you also need to use a model and a controller. First, create a Student object in the controller and modify it according to the ID of the record to be modified:

public function update()
{
    $id = 1;    //要修改的记录的ID
    $student = D('Student');    //实例化Student对象
    $data = array(
        'name' => 'Jerry',
        'age' => 20,
        'sex' => '男'
    );    //要修改的数据

    $student->where(array('id' => $id))->save($data);    //执行修改操作
}
Copy after login

In the save method, the first parameter can specify the conditions of the record to be modified, or not specified. If not specified, it will be modified based on the primary key.

The above is an introduction to addition, deletion and modification operations under the ThinkPHP framework. Database operations can be easily performed by using models and controllers. It is worth mentioning that when operating the database, attention should be paid to preventing SQL injection. Therefore, it is necessary to use the parameter binding method provided by the framework or strictly verify the parameters.

The above is the detailed content of How to perform addition, deletion and modification operations under the ThinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template