How can PHP be used to implement a voting system for images with unique IDs?

To implement a voting system for images with unique IDs in PHP, you can create a database table to store image IDs and their corresponding vote counts. When a user votes for an image, you can update the corresponding row in the database to increment the vote count. Make sure to sanitize user input to prevent SQL injection attacks.

<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "voting_system";

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

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

// Get image ID from user input
$image_id = $_POST['image_id'];

// Update vote count for image ID
$sql = "UPDATE images SET votes = votes + 1 WHERE id = $image_id";

if ($conn->query($sql) === TRUE) {
    echo "Vote counted successfully";
} else {
    echo "Error updating vote count: " . $conn->error;
}

$conn->close();
?>