Current Location: Home> Latest Articles> PHP Voting System Tutorial: From Database Design to Code Examples

PHP Voting System Tutorial: From Database Design to Code Examples

M66 2025-09-19

Introduction

With the rapid growth of the internet, online voting has become a common feature on websites and platforms. Using PHP, we can easily build a simple voting system that supports user participation, result statistics, and administrator management. This article will guide you through the entire process, from database design to code implementation.

System Requirements

The voting system includes the following features:

  • Users can view voting subjects and options
  • Users can cast a vote for their preferred option
  • Users can see the current votes for each option
  • Each user can only vote once
  • Administrators can create, edit, and delete voting subjects and options
  • Administrators can view voting results

Database Design

The following tables are needed to support these features:

  • vote_subjects: stores voting subjects with fields id, title, and created_at
  • vote_options: stores voting options with fields id, subject_id, option_name, and votes
  • votes: stores voting records with fields id, option_id, and ip_address

Code Implementation

Here are the main PHP code snippets for building the system:

Database connection:

<?php
$host = 'localhost';
$db = 'vote_system';
$user = 'root';
$pass = '';
$conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);
?>

Querying voting subjects and options:

<?php
$sql = 'SELECT * FROM vote_subjects';
$stmt = $conn->query($sql);
$subjects = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($subjects as $subject) {
    echo $subject['title'] . '<br>';
    $subject_id = $subject['id'];
    $sql = "SELECT * FROM vote_options WHERE subject_id = $subject_id";
    $stmt = $conn->query($sql);
    $options = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($options as $option) {
        echo $option['option_name'] . ': ' . $option['votes'] . ' votes<br>';
    }
}
?>

Voting logic:

<?php
$subject_id = $_POST['subject_id'];
$option_id = $_POST['option_id'];
$ip_address = $_SERVER['REMOTE_ADDR'];

$sql = "SELECT COUNT(*) FROM votes WHERE option_id = $option_id AND ip_address = '$ip_address'";
$stmt = $conn->query($sql);
$count = $stmt->fetchColumn();

if ($count > 0) {
    echo 'You have already voted!';
} else {
    $sql = "UPDATE vote_options SET votes = votes + 1 WHERE id = $option_id";
    $conn->exec($sql);

    $sql = "INSERT INTO votes (option_id, ip_address) VALUES ($option_id, '$ip_address')";
    $conn->exec($sql);

    echo 'Vote submitted successfully!';
}
?>

Administrator features:

<?php
// Create voting subject
$title = $_POST['title'];
$sql = "INSERT INTO vote_subjects (title) VALUES ('$title')";
$conn->exec($sql);

// Create voting option
$subject_id = $_POST['subject_id'];
$option_name = $_POST['option_name'];
$sql = "INSERT INTO vote_options (subject_id, option_name, votes) VALUES ($subject_id, '$option_name', 0)";
$conn->exec($sql);

// Edit voting option
$id = $_POST['id'];
$option_name = $_POST['option_name'];
$sql = "UPDATE vote_options SET option_name = '$option_name' WHERE id = $id";
$conn->exec($sql);

// Delete voting option
$id = $_POST['id'];
$sql = "DELETE FROM vote_options WHERE id = $id";
$conn->exec($sql);
?>

Conclusion

This article demonstrated how to implement a simple voting system using PHP. It covered database structure, user voting logic, and administrator operations. While this system provides basic functionality, a real-world implementation should also include security measures (such as SQL injection prevention), user authentication, and front-end optimization for better usability and reliability.