What are common mistakes when creating a PHP contact form?

One common mistake when creating a PHP contact form is not properly sanitizing user input, which can leave the form vulnerable to SQL injection attacks. To solve this issue, always use prepared statements when interacting with a database to prevent malicious code injection.

// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');

// Prepare a SQL statement using a prepared statement
$stmt = $pdo->prepare("INSERT INTO contacts (name, email, message) VALUES (:name, :email, :message)");

// Bind parameters and execute the statement
$stmt->bindParam(':name', $_POST['name']);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':message', $_POST['message']);
$stmt->execute();