use*_*655 14 php mysql mysqli lastinsertid
我想将图像与firstname,lastname相关联...如何检索最后一行并使用它插入另一个表?我试过$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();
Run Code Online (Sandbox Code Playgroud)
abe*_*abe 22
mysqli::$insert_id- mysqli_insert_id- 返回上一个查询中使用的自动生成的id,例如:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$mysqli->query("CREATE TABLE myCity LIKE City");
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);
printf ("New Record has id %d.\n", $mysqli->insert_id);
/* drop table */
$mysqli->query("DROP TABLE myCity");
/* close connection */
$mysqli->close();
Run Code Online (Sandbox Code Playgroud)
产量
新记录的ID为1.
参考
从 table2 中获取最后一行后,我想将其插入到 table1 中。这就是我所需要的
继续:
就如此容易
| 归档时间: |
|
| 查看次数: |
49306 次 |
| 最近记录: |