简体   繁体   English

如何获取一个表上的最后一个插入ID,以在另一个表上的另一个插入中使用它?

[英]How to get the last inserted id on a table to use it on another insert on another table?

Im trying to get id of the last inserted id on a table called authors to use it in another insert to a table called mail . 我试图获取名为authors的表上最后插入的id的id,以便在另一个名为mail的表的插入中使用它。

As you can see this is inserting a new author in table authors , and right after the insertion it must insert a welcome message to the new author in table mail . 如您所见,这是在表authors插入新authors ,并且插入后必须在表mail向新作者插入欢迎消息。

Im using mysql_insert_id() but its not working. 我正在使用mysql_insert_id()但无法正常工作。 Nothing is saved to database and no errors occurs. 什么都不会保存到数据库,也不会发生错误。

I know i should not use mysql_query but its ok, thats not the point here, just ignore this please. 我知道我不应该使用mysql_query,但是还可以,这不是重点,请忽略此内容。

if(!isset($row[email])){

    $sql = "INSERT INTO authors VALUES (NULL, 0, '".$email."', '".$username."', NULL, '".$first."', '".$fullname."', '".$first."', NULL, NULL, NULL, NULL,'http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."', 0);";
    mysql_query($sql);

    $sqlmsg = "INSERT INTO mail VALUES (NULL, 1, mysql_insert_id(), 'Welcome', Hello! Welcome to the website!', 'unread', NOW, 0, 0)";
    mysql_query($sqlmsg);

}   

Why nothing happens when a new author is inserted? 为什么插入新作者后什么也没发生?

Thank you. 谢谢。

您需要将其串联在字符串中,

$sqlmsg = "INSERT INTO mail VALUES (NULL, 1, " . mysql_insert_id() . ", 'Welcome', Hello! Welcome to the website!', 'unread', NOW, 0, 0)";

Try saving it on a temporary variable before using it on the next query. 尝试将其保存在一个临时变量上,然后在下一个查询中使用它。

<?php
if(!isset($row[email])){

  $sql = "INSERT INTO authors VALUES (NULL, 0, '".$email."', '".$username."', NULL, '".$first."', '".$fullname."', '".$first."', NULL, NULL, NULL, NULL,'http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."', 0);";
  mysql_query($sql);

  $author_id = mysql_insert_id();

  $sqlmsg = "INSERT INTO mail VALUES (NULL, 1, $author_id, 'Welcome', Hello! Welcome to the website!', 'unread', NOW, 0, 0)";
  mysql_query($sqlmsg);

}   
?>

Use a select query on the table and order by 'id' desc with limit 1 在表上使用选择查询,并按'id'desc进行排序(限制为1)

select `id` from `authors` order by `id desc limit 1

will return the last id if it's auto incremented. 如果自动递增,将返回最后一个ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一个表中插入最后插入的ID - How to Insert last inserted id in another table PHP检索插入的最后一条记录的ID,并将其插入另一个表 - php retrieving id of the last record inserted and insert it in another table 使用Codeigniter将最后插入的记录ID插入另一个表的数组中 - insert the last inserted record id into an array in another table using Codeigniter 想要将一个表的最后插入的ID插入laravel 5中的另一个表 - Want to insert one table's last inserted id into another table in laravel 5 如何将一个表的最后一个ID插入MySQL中的另一个表? - How to insert the last id from a table into another table in MySQL? 检索表的最后插入ID以在另一个表中用作外键 - retrieving last inserted ID of a table for using as Foreign key in another table 同一个查询中另一个表的表的最后插入 ID? - Last inserted ID of a table for another table in the same query? 获取Doctrine中最后插入的ID,并将该ID插入其子表 - Get Last Inserted Id in Doctrine and Insert that Id to its child table 从一个表中获取最后一个插入ID,然后放入另一个表中 - Grab the last insert id from one table to put into another table CodeIgniter:如何将$ this-&gt; db-&gt; insert_id()值传递给Controller以便插入到另一个表中? - CodeIgniter: How to pass $this->db->insert_id() value to Controller in order to be inserted in another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM