Current Location: Home> Latest Articles> PHP-Based Online Voting System with Diverse Voting Methods, Supporting Single-Choice, Multiple-Choice, and Rating

PHP-Based Online Voting System with Diverse Voting Methods, Supporting Single-Choice, Multiple-Choice, and Rating

M66 2025-06-12

PHP-Based Online Voting System with Diverse Voting Methods

With the advent of the internet era, people's demand for participation and expressing opinions has been growing stronger. The online voting system has become a quick and convenient way to collect group opinions and facilitate democratic decision-making.

In this article, we will introduce how to implement an online voting system based on PHP that supports diverse voting methods and provide complete code examples to help developers build their own voting platform.

Voting System Requirement Analysis

Before designing the online voting system, we need to clarify the system requirements. Below is a brief analysis of the requirements:
  1. Support for single-choice, multiple-choice, and rating voting methods;

  2. Users can vote anonymously;

  3. Administrators can create voting topics, edit options, and set voting times;

  4. The system can calculate and display voting results in real-time.

Database Design

To store voting data, we create two main tables in MySQL: the `vote` table stores voting topic information, and the `option` table stores voting options.

Below is a structure example for these two tables:

CREATE TABLE vote (
  id int(11) NOT NULL AUTO_INCREMENT,
  title varchar(255) NOT NULL,
  start_time datetime NOT NULL,
  end_time datetime NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE option (
  id int(11) NOT NULL AUTO_INCREMENT,
  vote_id int(11) NOT NULL,
  content text NOT NULL,
  PRIMARY KEY (id),
  KEY vote_id (vote_id),
  CONSTRAINT fk_vote_id FOREIGN KEY (vote_id) REFERENCES vote (id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Front-End Design

The front-end design focuses on displaying the voting topic, allowing users to select voting options, and showing the voting results. Below is a simple front-end implementation example:
// Displaying the voting topic
echo "<h2>" . $vote['title'] . "</h2>";

// Option selection
foreach ($options as $option) {
  echo "<input type='" . $vote['type'] . "' name='option' value='" . $option['id'] . "'>" . $option['content'] . "<br/>";
}

// Submit button
echo "<input type='submit' value='Submit Vote'>";

Back-End Implementation

The main task of the back-end is to handle the user's voting data submission and update the voting results in real-time. Below is a simplified PHP code example:
// Submit vote
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $selectedOption = $_POST['option'];

  // Handle according to option type
  if ($vote['type'] == 'radio') {
    // Single-choice vote
    $voteCount = 1;
  } elseif ($vote['type'] == 'checkbox') {
    // Multiple-choice vote
    $voteCount = count($selectedOption);
  } elseif ($vote['type'] == 'rating') {
    // Rating vote
    $totalRating = $_POST['total_rating'];
    $voteCount = 1;
  }

  // Update voting statistics
  foreach ($selectedOption as $optionId) {
    // Perform update operation: UPDATE option SET count = count + 1 WHERE id = $optionId
  }
}

Query Voting Results

After the voting ends, the system calculates and displays the percentage of votes for each option based on the number of votes it received:
// Query voting results
$options = $db->query("SELECT * FROM option WHERE vote_id = ".$vote['id'])->fetchAll();
foreach ($options as $option) {
  $votePercentage = ($option['count'] / $voteCount) * 100;
  echo $option['content'] . ": " . $votePercentage . "%<br/>";
}

Conclusion

Through the code examples above, we can build an online voting system that supports diverse voting methods. This system meets users' voting needs and provides administrators with real-time voting statistics. Developers can further customize the system according to their specific needs, adding more voting options and functionalities, making the online voting system more flexible and practical.