What are some common pitfalls to avoid when writing PHP code for form submissions?

One common pitfall to avoid when writing PHP code for form submissions is not properly sanitizing user input, which can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To solve this issue, always use functions like htmlspecialchars() or mysqli_real_escape_string() to sanitize user input before using it in database queries or displaying it on the webpage. Example PHP code snippet:

// Sanitize user input before using it in a SQL query
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$message = mysqli_real_escape_string($conn, $_POST['message']);

// Insert sanitized data into the database
$sql = "INSERT INTO messages (name, email, message) VALUES ('$name', '$email', '$message')";
$result = mysqli_query($conn, $sql);

if ($result) {
    echo "Message sent successfully!";
} else {
    echo "Error: " . mysqli_error($conn);
}