What are common pitfalls when dealing with POST data in PHP, especially when handling special characters like quotes?

When dealing with POST data in PHP, a common pitfall is not properly sanitizing the input, especially when handling special characters like quotes. To avoid issues like SQL injection or cross-site scripting attacks, it is important to use functions like htmlspecialchars() or mysqli_real_escape_string() to sanitize the input before using it in your code.

// Sanitize POST data to prevent SQL injection
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);

// Connect to database
$mysqli = new mysqli('localhost', 'username', 'password', 'database');

// Sanitize input before using in query
$username = $mysqli->real_escape_string($username);
$password = $mysqli->real_escape_string($password);

// Query database
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = $mysqli->query($query);

// Handle query result
if ($result->num_rows > 0) {
    // User authenticated successfully
} else {
    // User authentication failed
}

// Close database connection
$mysqli->close();