Home >Database >Mysql Tutorial >How to Retrieve the Last Inserted ID to Populate Another Table?
In your code snippet, you're attempting to retrieve the last inserted ID from one table and use it to insert data into another. However, the line where you assign the value to $image is incorrect. Here's how you can correctly retrieve the last inserted ID:
$image = mysqli_insert_id($mysqli);
$stmt = $mysqli->prepare(" insert into table1 (username, firstname, lastname, image) select ?,?,?,? from table2 t2 where username = ? and t2.id = ? "); $stmt->bind_param('sssss', $username, $fname, $lname, $image, $username, $image); $stmt->execute();
The above is the detailed content of How to Retrieve the Last Inserted ID to Populate Another Table?. For more information, please follow other related articles on the PHP Chinese website!