What are some common pitfalls when creating dynamic HTML tables with PHP?

One common pitfall when creating dynamic HTML tables with PHP is not properly escaping user input, which can lead to security vulnerabilities like SQL injection attacks. To solve this issue, always use prepared statements or parameterized queries when interacting with a database to prevent malicious input.

// Example of using prepared statements to prevent SQL injection
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->execute(['username' => $_POST['username']]);
$results = $stmt->fetchAll();
foreach ($results as $row) {
    echo "<tr>";
    echo "<td>" . htmlspecialchars($row['username']) . "</td>";
    echo "<td>" . htmlspecialchars($row['email']) . "</td>";
    echo "</tr>";
}