Home  >  Article  >  Backend Development  >  How to use PHP to develop a simple voting system and result statistics function

How to use PHP to develop a simple voting system and result statistics function

WBOY
WBOYOriginal
2023-09-21 13:01:071373browse

How to use PHP to develop a simple voting system and result statistics function

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).

  1. Voting option table (options):

    • id (int): the unique identifier of the voting option
    • option_name (varchar): voting The name of the option
    • vote_count (int): The number of votes for this option
  2. Voting results table (votes):

    • id (int): The unique identification of the voting result
    • option_id (int): The id of the selected voting option
    • voter_ip (varchar): The IP address of the voter

4. Code implementation:

  1. Connecting to the database:
    First, we need to connect to the database in PHP. Create a file called "db_connect.php" and write the following code:
<?php
$servername = "localhost"; // 数据库服务器名称
$username = "root"; // 数据库用户名
$password = ""; // 数据库密码
$dbname = "vote_system"; // 数据库名

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("连接失败:" . $conn->connect_error);
}
?>
  1. Create voting options:
    In the page, we can use an HTML form to create voting options . Create a file named "index.php" and write the following code:
<!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>
  1. Processing the voting results:
    Next, we need to write the processing logic for the voting results. Create a file called "vote.php" and write the following code:
<?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();
?>
  1. Display the voting results:
    Finally, we need to write the code to display the voting results. Create a file named "result.php" and write the following code:
<?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!

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

Related articles

See more