How can the code in the vote.php file be optimized for better performance and readability?

The code in the vote.php file can be optimized for better performance and readability by implementing error handling, using prepared statements to prevent SQL injection, and organizing the code into smaller, more manageable functions. This will make the code more robust, secure, and easier to understand.

<?php

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

// Check if the form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['vote'])) {
        $vote = $_POST['vote'];

        // Prepare and execute the SQL statement using a prepared statement
        $stmt = $pdo->prepare("INSERT INTO votes (vote) VALUES (:vote)");
        $stmt->bindParam(':vote', $vote);
        $stmt->execute();

        // Check if the query was successful
        if ($stmt->rowCount() > 0) {
            echo 'Vote submitted successfully!';
        } else {
            echo 'Error submitting vote.';
        }
    } else {
        echo 'No vote data submitted.';
    }
} else {
    echo 'Invalid request method.';
}