What is the significance of the affected_rows function in PHP when dealing with database inserts?

The affected_rows function in PHP is significant when dealing with database inserts as it returns the number of rows affected by the most recent query. This can be useful for error checking and validation purposes to ensure that the insert operation was successful.

// Perform the database insert query
$result = $mysqli->query("INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')");

// Check the number of affected rows
if ($mysqli->affected_rows > 0) {
    echo "Insert operation successful!";
} else {
    echo "Error: Insert operation failed.";
}