php - mysqli 最后插入 ID

标签 php mysql mysqli lastinsertid

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

 $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);

关于php - mysqli 最后插入 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32808207/

相关文章:

php - Mysql查询性能在哪里?

mysql - 使用IP地址连接到MySql

php - Mysql 使用 PHP 连接表 - 没有任何运气

php - 在我的 ApacheBench 负载测试结果中按长度失败的请求

PHP 脚本和 HTML - 这适用于 Firefox 和 Chrome,但不适用于 IE

php - codeigniter - 未初始化的字符串偏移量 : 0 and Illegal string offset

java - Hibernate 批量插入在 MySql 中不起作用

php - 不使用 mysqli 处理 MySQL 事务

php - MySQLi 从表中选择主题标签

php - 如何以二进制安全的方式从多字节 PHP 字符串变量中获取单个字节?