What are common pitfalls when writing a voting script in PHP?

One common pitfall when writing a voting script in PHP is not properly sanitizing user input, which can lead to SQL injection attacks. To solve this issue, always use prepared statements or parameterized queries when interacting with a database to prevent malicious input.

// Example of using prepared statements to prevent SQL injection

// Assuming $conn is the database connection object

$vote = $_POST['vote']; // Assuming this is the user's vote input

$stmt = $conn->prepare("INSERT INTO votes (vote) VALUES (?)");
$stmt->bind_param("s", $vote);
$stmt->execute();
$stmt->close();