I would like to associate the image with firstname, lastname...how can I retrieve the last rowand use it to insert to the other table? I tried $image = $mysqli->insert_id;
then binding but it doesn't work. Can someone help me out?
$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();
insert_id;
after you executed query with insert, like:if ($stmt->execute()) $image = $stmt->insert_id;
– Resurrectt2.id = ?
– Womanizerinsert into table1 (username, firstname, lastname, image) select ?,?,?,image from table2 t2 where username = ? order by t2.id desc limit1
– Resurrectorder by t2.id desc limit 1
sorts in desc order, which means that only last id in table will be selected – Resurrect