How to use PHP to develop a simple voting system and result statistics function?
1. Introduction:
The voting system is a common function widely used in various scenarios, such as corporate employee selection, student representative election, etc. This article will introduce how to use PHP to develop a simple voting system and implement the result statistics function.
2. Set up the environment:
Before starting, you need to ensure that the PHP environment has been installed locally or on the server. If it is not installed, you can refer to the relevant documents for installation and configuration.
3. Database design:
Before you start writing code, you need to design a database table to store voting-related data. Suppose we need to implement a voting system, which involves two tables: voting options table (options) and voting results table (votes).
Voting option table (options):
Voting results table (votes):
4. Code implementation:
<?php $servername = "localhost"; // 数据库服务器名称 $username = "root"; // 数据库用户名 $password = ""; // 数据库密码 $dbname = "vote_system"; // 数据库名 $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败:" . $conn->connect_error); } ?>
<!DOCTYPE html> <html> <head> <title>投票系统</title> </head> <body> <h2>请选择您的投票选项:</h2> <form action="vote.php" method="post"> <?php require_once 'db_connect.php'; $sql = "SELECT * FROM options"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "<input type='radio' name='option_id' value='" . $row['id'] . "'>" . $row['option_name'] . "<br>"; } } $conn->close(); ?> <br> <input type="submit" value="提交"> </form> </body> </html>
<?php require_once 'db_connect.php'; $option_id = $_POST['option_id']; $voter_ip = $_SERVER['REMOTE_ADDR']; // 检查是否已经投过票 $sql = "SELECT * FROM votes WHERE voter_ip = '$voter_ip'"; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "您已经投过票了!"; } else { // 更新投票选项的数量 $sql = "UPDATE options SET vote_count = vote_count + 1 WHERE id = $option_id"; $conn->query($sql); // 保存投票结果到数据库 $sql = "INSERT INTO votes (option_id, voter_ip) VALUES ($option_id, '$voter_ip')"; $conn->query($sql); echo "投票成功!"; } $conn->close(); ?>
<?php require_once 'db_connect.php'; $sql = "SELECT * FROM options"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo $row['option_name'] . ":" . $row['vote_count'] . " 票<br>"; } } else { echo "暂无投票结果!"; } $conn->close(); ?>
5. Test run:
Visit the "index.php" page in the browser to proceed Vote and see the results.
Summary:
This article introduces how to use PHP to develop a simple voting system and implement the statistical function of voting results. Through the examples in this article, you can learn how to connect to the database, create voting options, process voting results, and display voting results. I hope this article will help you understand the development of voting systems in PHP.
The above is the detailed content of How to use PHP to develop a simple voting system and result statistics function. For more information, please follow other related articles on the PHP Chinese website!