ThinkPHP write array insertion and get the latest inserted data ID example, thinkphp example
The example in 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 analysis is as follows:
This example describes how thinkphp writes array insertion by itself. Here is a registered user as an example.
The specific implementation code is as follows:
Copy code The code is as follows:
public function insert2(){
header("Content-Type:text/html; charset=utf-8");
$Dao = M("User"); // Construct the written data array
$data["username"] = $_POST["username"];
$data["password"] = md5($_POST["password"];);
$data["email"] = $_POST["email"];
$data["regdate"] = time(); // Write data
if($lastInsId = $Dao->add($data)){ //$lastInsId is the id of the latest inserted data
echo "Insert data id is: $lastInsId";
} else {
$this->error('Data writing error!');
}
}
I hope this article will be helpful to everyone’s ThinkPHP framework programming.
It’s very simple, if you have inserted successfully
$res=executes the insert statement;
if($res)
{
$session['id']=$res[ 'id']
Perform successful operation
}
Generally inserting a piece of data will return the id of the data. You can directly define a variable to store it.
$map is an array
$id = D('User')->add( $map);
This $id is the id inserted into this data
http://www.bkjia.com/PHPjc/904923.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/904923.htmlTechArticleThinkPHP writes array insertion and gets the latest inserted data ID example, thinkphp example This article tells the thinkphp write array insert and get The latest implementation method of inserting data ID. Share with everyone...