How can the issue of inserting an additional empty record be resolved in the PHP code?

Issue: The problem of inserting an additional empty record can be resolved by checking if the form fields are empty before inserting the record into the database. PHP Code Snippet:

// Check if form fields are not empty before inserting into the database
if(!empty($_POST['field1']) && !empty($_POST['field2'])) {
    // Insert record into the database
    $query = "INSERT INTO table_name (field1, field2) VALUES ('".$_POST['field1']."', '".$_POST['field2']."')";
    // Execute the query
    $result = mysqli_query($connection, $query);
} else {
    // Handle the case where form fields are empty
    echo "Please fill in all the fields.";
}