What are common pitfalls to watch out for when inserting user inputs into a MySQL table using PHP?

One common pitfall when inserting user inputs into a MySQL table using PHP is SQL injection attacks. To prevent this, always sanitize and validate user inputs before inserting them into the database. Use prepared statements with parameterized queries to securely insert user inputs.

// Assuming $conn is the MySQL database connection

// Sanitize and validate user inputs
$username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);

// Prepare a SQL statement with parameterized query
$stmt = $conn->prepare("INSERT INTO users (username, email) VALUES (?, ?)");
$stmt->bind_param("ss", $username, $email);

// Execute the statement
$stmt->execute();

// Close the statement and connection
$stmt->close();
$conn->close();