How can PHP be used to store and manage user voting history across multiple polls or surveys?

To store and manage user voting history across multiple polls or surveys in PHP, you can create a database table to store the user ID, poll ID, and their vote. When a user votes on a poll, you can insert a record into this table. To prevent duplicate votes, you can check if the user has already voted on a particular poll before allowing them to vote again.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "voting_history";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Check if the user has already voted on a poll
$user_id = 1; // User ID
$poll_id = 1; // Poll ID

$sql = "SELECT * FROM user_votes WHERE user_id = $user_id AND poll_id = $poll_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "You have already voted on this poll.";
} else {
    // Insert the user's vote into the database
    $vote = "Option A"; // User's vote
    $sql = "INSERT INTO user_votes (user_id, poll_id, vote) VALUES ($user_id, $poll_id, '$vote')";
    
    if ($conn->query($sql) === TRUE) {
        echo "Vote recorded successfully.";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

$conn->close();