ThinkPHP database operation-new data

1. New single data

1.1 Use the insert method to add data

  • Use the insert() method to add a piece of data to the data table, and use the default for more fields.

  • If the insertion is successful, a 1 value will be returned.

  • If you insert a field that does not exist, an exception will be thrown.

  • If you want to forcibly add and discard the non-existent field data, use the strick(false) method and ignore the exception.

use think\facade\Db;

class DataBaseTest
{
    
    
    public function insert()
    {
    
    
        $data = [
            'username' => 'jiangxiaoju',
            'password' => '123',
            'gender' => '男',
            'email' => '[email protected]',
            'price' => 90,
            'details' => '123'
        ];
        $res = Db::name('user')->insert($data);
//        $res = Db::name('user')->strict(false)->insert($data);
        return json($res);
    }
}

Use the insertGetId() method to return the ID of the current data after the addition is successful.

use think\facade\Db;

class DataBaseTest
{
    
    
    public function insert()
    {
    
    
        $data = [
            'username' => 'jiangxiaoju2',
            'password' => '123',
            'gender' => '男',
            'email' => '[email protected]',
            'price' => 90,
            'details' => '123'
        ];
        $res = Db::name('user')->replace()->insert($data);
        return Db::name('user')->insertGetId($data);
    }
}

1.2 Use the save method to add.

The save() method is a general method, you can judge by yourself whether to add or modify (update) the data; if it does not exist, add it gradually, and vice versa.

use think\facade\Db;

class DataBaseTest
{
    
    
    public function insert()
    {
    
    
        $data = [
            'id' => 302,
            'username' => 'jiangxiaoju2',
            'password' => '123',
            'gender' => '男',
            'email' => '[email protected]',
            'price' => 90,
            'details' => '123'
        ];
         $res = Db::name('user')->save($data );
         echo Db::getLastSql();
    }
}

2. New batch data

2.1 Use insertAll() method to batch insert

Use the insertAll() method to insert data in batches, but the array structure must be maintained.

use think\facade\Db;

class DataBaseTest
{
    
    
    public function insert()
    {
    
    
        $data = [
            [
                'username' => 'jiangxiaoju3',
                'password' => '123',
                'gender' => '男',
                'email' => '[email protected]',
                'price' => 90,
                'details' => '123'
            ],
            [
                'username' => 'jiangxiaoju4',
                'password' => '123',
                'gender' => '男',
                'email' => '[email protected]',
                'price' => 90,
                'details' => '123'
            ],
            [
                'username' => 'jiangxiaoju5',
                'password' => '123',
                'gender' => '男',
                'email' => '[email protected]',
                'price' => 90,
                'details' => '123'
            ],
        ];
        $res = Db::name('user')->insertAll($data);
        return $res;
    }
}

Guess you like

Origin blog.csdn.net/qq_43058685/article/details/112516283