Home > Backend Development > PHP Tutorial > How to use PHP to implement online voting and polling functions

How to use PHP to implement online voting and polling functions

PHPz
Release: 2023-09-05 10:58:01
Original
890 people have browsed it

如何使用 PHP 实现在线投票和民意调查功能

How to use PHP to implement online voting and opinion poll functions

In modern society, opinion polls and online voting have become important means of obtaining public opinions and decision-making. As a programming language widely used in web development, PHP provides us with a wealth of tools and functions to achieve such functions. This article will introduce you to how to use PHP to implement online voting and polling functions.

  1. Database design

First, we need to design a database to store voting and survey-related data. We can create a table called "polls" to store voting information, including the poll title, options, and the number of votes for each option. The sample code is as follows:

CREATE TABLE polls (

id INT(11) AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
options TEXT NOT NULL,
votes TEXT DEFAULT '{"option1":0, "option2":0}'
Copy after login

);

  1. Display survey and voting form

Next, we A PHP page can be created to display currently available surveys and polling forms. We can use the SELECT statement to get the list of surveys from the database and display it on the page. The sample code is as follows:

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

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {

die("连接失败: " . $conn->connect_error);
Copy after login
Copy after login
Copy after login

}

// Get the survey list
$sql = "SELECT * FROM polls";
$result = $conn->query($sql);

// Display survey list and voting form
if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
    echo "<h2>".$row["title"]."</h2>";
    $options = json_decode($row["options"]);
    echo "<form action="vote.php" method="POST">";
    foreach ($options as $option) {
        echo "<input type="radio" name="option" value="$option">$option<br>";
    }
    echo "<input type="hidden" name="poll_id" value="".$row["id"]."">";
    echo "<input type="submit" value="投票">";
    echo "</form>";
}
Copy after login

} else {

echo "暂无可用的调查。";
Copy after login
Copy after login

}
$ conn->close();
?>

  1. Handling voting requests

When the user submits the voting form, we need to create a PHP script to handle it A vote request is made and the vote counter in the database is updated. The sample code is as follows:

// Get voting options and survey ID
$option = $_POST["option"];
$poll_id = $_POST["poll_id "];

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

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {

die("连接失败: " . $conn->connect_error);
Copy after login
Copy after login
Copy after login

}

// Get current voting information
$sql = "SELECT * FROM polls WHERE id = $poll_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {

$row = $result->fetch_assoc();
$votes = json_decode($row["votes"], true);

// 更新投票计数器
$votes[$option]++;
$votes_json = json_encode($votes);

// 更新数据库中的投票信息
$sql = "UPDATE polls SET votes = '$votes_json' WHERE id = $poll_id";
if ($conn->query($sql) === TRUE) {
    echo "投票成功!";
} else {
    echo "投票失败: " . $conn->error;
}
Copy after login

} else {

echo "调查不存在。";
Copy after login

}

$conn->close();
?> ;

  1. Display voting results

Finally, we need to create a PHP page to display the voting results. We can use the SELECT statement to get the voting information from the database and display it on the page. The sample code is as follows:

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

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {

die("连接失败: " . $conn->connect_error);
Copy after login
Copy after login
Copy after login

}

// Get voting information
$sql = "SELECT * FROM polls";
$result = $conn->query($sql);

// Display voting results
if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
    echo "<h2>".$row["title"]."</h2>";
    $votes = json_decode($row["votes"], true);
    $total_votes = array_sum($votes);
    foreach ($votes as $option => $vote_count) {
        $percentage = ($vote_count / $total_votes) * 100;
        echo "<p>$option : $vote_count 票 ($percentage%)</p>";
    }
}
Copy after login

} else {

echo "暂无可用的调查。";
Copy after login
Copy after login

}
$conn-> ;close();
?>

Through the above steps, we can use PHP to implement online voting and opinion poll functions. You can expand and customize it according to your needs, such as adding verification, permission control and other functions to meet the requirements of specific projects.

Hope this article is helpful to you!

The above is the detailed content of How to use PHP to implement online voting and polling functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template