What is the best practice for retrieving the last inserted ID from a database in PHP and using it in subsequent queries?

When inserting a record into a database in PHP, you can retrieve the last inserted ID using the `mysqli_insert_id()` function. This ID can then be used in subsequent queries, such as updating related tables or displaying information to the user.

// Insert a record into the database
$query = "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')";
$result = mysqli_query($connection, $query);

// Get the last inserted ID
$last_inserted_id = mysqli_insert_id($connection);

// Use the last inserted ID in subsequent queries
$query2 = "UPDATE other_table SET column3 = 'new_value' WHERE id = $last_inserted_id";
$result2 = mysqli_query($connection, $query2);