Home >Backend Development >PHP Tutorial >THINKPHP method to obtain the value of the primary key id when adding data
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()); }
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!