THINKPHP method to obtain the value of the primary key id when adding data

不言
Release: 2023-03-25 11:18:01
Original
2400 people have browsed it

This article mainly introduces the method of THINKPHP to obtain the value of the primary key id when adding data. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

When using ThinkPHP After adding new data, you can easily obtain the automatically growing primary key value.

$Model = D(‘Blog');

$data['name'] = 'test';

$data['title'] = '测试标题';

$data['content'] = '测试内容';

$result = $Model->add($data);

if ($result){

  $id = $result; // 获取数据库写入数据的主键

}else{

  exit($Model->getError());

}
Copy after login

If the primary key of your data table is auto-growing, then the return value after the add method succeeds is the primary key value of the data. No additional acquisition required.

Related recommendations:

ThinkPHP method of adding update tag_php example

# #

The above is the detailed content of THINKPHP method to obtain the value of the primary key id when adding data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!