Home >Backend Development >PHP Tutorial >How to develop online voting function using PHP

How to develop online voting function using PHP

王林
王林Original
2023-08-17 13:06:181824browse

How to develop online voting function using PHP

How to use PHP to develop online voting functions

In modern society, online voting has become a common way to collect public opinions and make decisions. PHP, as a popular server-side scripting language, can be easily used to develop online voting functions. This article will introduce how to use PHP to develop a simple online voting system, and attach corresponding code examples.

  1. Database design

First, we need to design a database for storing voting information. Suppose we need to create a database named "votes" and create a data table named "polls" to store voting option information. The structure of the data table "polls" is as follows:

polls table

id (INT) -- voting option ID
name (VARCHAR) -- voting option name
votes (INT ) -- Number of votes

  1. Create voting options

Display voting options on the web page for users to choose, which can be achieved through the following PHP code:

<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "votes";
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查数据库连接是否成功
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

// 查询投票选项
$sql = "SELECT id, name FROM polls";
$result = $conn->query($sql);

// 展示投票选项
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<input type='radio' name='option' value='" . $row["id"] . "'>" . $row["name"] . "<br>";
    }
} else {
    echo "没有投票选项可供选择";
}

// 关闭数据库连接
$conn->close();
?>
  1. Processing voting results

Next, we need to process the user's voting selections and update the number of votes in the database. The following is the relevant PHP code example:

<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "votes";
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查数据库连接是否成功
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

// 获取用户的投票选择
$optionID = $_POST["option"];

// 更新投票数
$sql = "UPDATE polls SET votes = votes + 1 WHERE id = " . $optionID;

if ($conn->query($sql) === TRUE) {
    echo "投票成功";
} else {
    echo "投票失败: " . $conn->error;
}

// 关闭数据库连接
$conn->close();
?>
  1. Display voting results

Finally, we can use the following PHP code to display the results of the current voting options:

<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "votes";
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查数据库连接是否成功
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

// 查询投票结果
$sql = "SELECT name, votes FROM polls";
$result = $conn->query($sql);

// 展示投票结果
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo $row["name"] . ":" . $row["votes"] . "票<br>";
    }
} else {
    echo "目前还没有投票结果";
}

// 关闭数据库连接
$conn->close();
?>

Through the above steps, we successfully developed a simple online voting function using PHP. You can make further improvements and extensions based on actual needs, such as adding a voting deadline, limiting the number of votes per user, etc. I wish you success in developing an online voting system that suits you!

The above is the detailed content of How to develop online voting function using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn