How can one retrieve the ID of the last inserted record in PHP?

To retrieve the ID of the last inserted record in PHP, you can use the mysqli_insert_id() function which returns the auto-generated ID used in the last query. This function should be called immediately after the INSERT query to ensure you get the correct ID.

// Perform the INSERT query
mysqli_query($connection, "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')");

// Get the ID of the last inserted record
$last_insert_id = mysqli_insert_id($connection);

echo "The ID of the last inserted record is: " . $last_insert_id;