Mysqli 最后插入 ID


mysqli last insert id

我想将图像与名字,姓氏相关联...如何检索最后一行并使用它来插入到另一个表中?我尝试$image = $mysqli->insert_id;然后绑定,但它不起作用。有人可以帮助我吗?

 $image = $mysqli->insert_id;//this should come from table2
 $stmt = $mysqli->prepare("
  insert into table1 (username, firstname, lastname, image) 
  select ?,?,?,image from table2 t2 where username = ? and  t2.id = ? 
   ");
 $stmt->bind_param('sssss', $username, $fname, $lname, $username, $image);
 $stmt->execute();

首先,您需要在ID中创建auto_increment字段

然后你可以使用 $last_id = mysqli_insert_id($conn);

从表2获得最后一行后,我想将其插入表1。这就是我所需要的

继续:

  1. 使用简单的常规插入查询插入表 1
  2. 获取上次插入 ID
  3. 使用简单的常规插入查询插入表 2

就这么简单