Home  >  Article  >  Backend Development  >  ThinkPHP writes array insertion and gets the latest inserted data ID method

ThinkPHP writes array insertion and gets the latest inserted data ID method

不言
不言Original
2018-07-03 16:28:433045browse

This article mainly introduces ThinkPHP's method of writing array insertion and obtaining the latest inserted data ID. The example describes ThinkPHP's method of operating the database based on arrays, which has certain reference value. Friends in need can refer to it

The example of this article describes the implementation method of thinkphp writing array insertion and obtaining the latest inserted data ID. Share it with everyone for your reference. The specific method is analyzed as follows:

This example describes how thinkphp writes array insertion by itself. Here, a registered user is used as an example.

The specific implementation code is as follows:

public function insert2(){ 
 header("Content-Type:text/html; charset=utf-8"); 
 $Dao = M("User"); // 构建写入的数据数组
 $data["username"] = $_POST["username"];
 $data["password"] = md5($_POST["password"];);
 $data["email"] = $_POST["email"];
 $data["regdate"] = time(); // 写入数据
 if($lastInsId = $Dao->add($data)){  //$lastInsId为最新插入数据的id
 echo "插入数据 id 为:$lastInsId";
 } else {
 $this->error('数据写入错误!');
 }
 }

The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Introduction to the operation classes and usage of PHP’s full-featured non-deformation image cropping

ThinkPHP3.1 A more complete introduction to the new features’ support for Ajax

The above is the detailed content of ThinkPHP writes array insertion and gets the latest inserted data ID method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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