What are the potential reasons for PHP creating empty rows in a MySQL table when inserting data from an XML file?

The potential reasons for PHP creating empty rows in a MySQL table when inserting data from an XML file could be due to incorrect parsing of the XML file, missing data in the XML file, or errors in the SQL query used to insert the data. To solve this issue, ensure that the XML file is properly parsed to extract the necessary data and that the SQL query is correctly constructed to insert the data into the MySQL table.

<?php

// Load the XML file
$xml = simplexml_load_file('data.xml');

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Loop through each record in the XML file and insert data into MySQL table
foreach ($xml->record as $record) {
    $name = $record->name;
    $age = $record->age;

    // Insert data into MySQL table
    $query = "INSERT INTO table_name (name, age) VALUES ('$name', '$age')";
    $mysqli->query($query);
}

// Close the database connection
$mysqli->close();

?>