What are common pitfalls when creating a form submission in PHP and how can they be avoided?

One common pitfall when creating a form submission in PHP is not properly sanitizing user input, which can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To avoid this, always use functions like htmlspecialchars() or mysqli_real_escape_string() to sanitize user input before using it in SQL queries or displaying it on the page.

// Sanitize user input using htmlspecialchars()
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);

// Use sanitized input in SQL query
$query = "INSERT INTO messages (name, email, message) VALUES ('$name', '$email', '$message')";
mysqli_query($connection, $query);