在TP框架中,数据插入
添加一条数据,返回添加成功的条数
$data = ['foo' => 'bar', 'bar' => 'foo'];
Db::name('user')->save($data);
// 或者
Db::name('user')->insert($data);
批量添加
$data = [['foo' => 'bar', 'bar' => 'foo'],['foo' => 'bar1', 'bar' => 'foo1'],['foo' => 'bar2', 'bar' => 'foo2']
];
Db::name('user')->insertAll($data);
如果要返回批量添加是生成的主键ID,使用saveAll
$user = new User;
$list = [['name'=>'thinkphp','email'=>'thinkphp@qq.com'],['name'=>'onethink','email'=>'onethink@qq.com']
];
$res = $user->saveAll($list);$ids = [];
if($res){foreach($res as $v){$ids[] = $v['id'];}
}
// 最后得到所有id
var_dump($ids);