How to use PHP to develop a simple online voting system
Voting is a very common activity, whether in schools, companies or societies, it is often done Organize various voting activities. With the development of the Internet, many voting activities can now be conducted online. This article will introduce how to use PHP to develop a simple online voting system and provide specific code examples.
1. Database design
First, we need to design the database to store voting-related information. Suppose we need to design two tables: voting subject table (vote_subject) and voting option table (vote_option).
The voting subject table needs to contain the following fields:
The voting option table needs to contain the following fields:
2. Create a voting topic
First, we need to create a page for users to create new voting topics. On this page, users can enter a topic name and submit the form.
Sample code:
<!DOCTYPE html> <html> <head> <title>创建投票主题</title> </head> <body> <h1>创建投票主题</h1> <form action="create_subject.php" method="post"> <label for="subject_name">主题名称:</label> <input type="text" name="subject_name" id="subject_name" required> <br><br> <input type="submit" value="创建"> </form> </body> </html>
In the create_subject.php file, we can receive the form data submitted by the user, then insert the topic name into the voting topic table and generate a new topic ID.
Sample code:
<?php // 连接数据库 $servername = "localhost"; $username = "root"; $password = "123456"; $dbname = "vote_system"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接数据库失败:" . $conn->connect_error); } // 接收用户提交的表单数据 $subject_name = $_POST['subject_name']; // 将主题名称插入投票主题表 $sql = "INSERT INTO vote_subject (subject_name, create_time) VALUES ('$subject_name', NOW())"; if ($conn->query($sql) === TRUE) { $subject_id = $conn->insert_id; // 获取新的主题ID echo "创建投票主题成功,主题ID为:" . $subject_id; } else { echo "创建投票主题失败:" . $conn->error; } // 关闭数据库连接 $conn->close(); ?>
3. Add voting options
Next, we need to create a page to add voting options. In this page, the user needs to enter the option name and select the topic to which it belongs.
Sample code:
<!DOCTYPE html> <html> <head> <title>添加投票选项</title> </head> <body> <h1>添加投票选项</h1> <form action="add_option.php" method="post"> <label for="option_name">选项名称:</label> <input type="text" name="option_name" id="option_name" required> <br><br> <label for="subject_id">所属主题:</label> <select name="subject_id" id="subject_id"> <?php // 查询所有的投票主题 $sql = "SELECT * FROM vote_subject"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "<option value=" . $row['subject_id'] . ">" . $row['subject_name'] . "</option>"; } } ?> </select> <br><br> <input type="submit" value="添加"> </form> </body> </html>
In the add_option.php file, we can receive the form data submitted by the user, and then insert the option name and the subject it belongs to into the voting options table.
Sample code:
<?php // 连接数据库(同上) // 接收用户提交的表单数据 $option_name = $_POST['option_name']; $subject_id = $_POST['subject_id']; // 将选项名称和所属主题插入投票选项表 $sql = "INSERT INTO vote_option (subject_id, option_name, option_count) VALUES ('$subject_id', '$option_name', 0)"; if ($conn->query($sql) === TRUE) { echo "添加投票选项成功"; } else { echo "添加投票选项失败:" . $conn->error; } // 关闭数据库连接(同上) ?>
4. Voting operation
Finally, we need to create a page for users to vote. From this page, users can select a topic and then choose an option to vote on.
Sample code:
<!DOCTYPE html> <html> <head> <title>进行投票</title> </head> <body> <h1>进行投票</h1> <form action="vote.php" method="post"> <label for="subject_id">选择主题:</label> <select name="subject_id" id="subject_id"> <?php // 查询所有的投票主题 $sql = "SELECT * FROM vote_subject"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "<option value=" . $row['subject_id'] . ">" . $row['subject_name'] . "</option>"; } } ?> </select> <br><br> <label for="option_id">选择选项:</label> <select name="option_id" id="option_id"> <?php // 查询指定主题下的所有选项 $subject_id = $_POST['subject_id']; $sql = "SELECT * FROM vote_option WHERE subject_id = $subject_id"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "<option value=" . $row['option_id'] . ">" . $row['option_name'] . "</option>"; } } ?> </select> <br><br> <input type="submit" value="投票"> </form> </body> </html>
In the vote.php file, we can receive the form data submitted by the user and then update the option vote number based on the selected theme and option.
Sample code:
<?php // 连接数据库(同上) // 接收用户提交的表单数据 $subject_id = $_POST['subject_id']; $option_id = $_POST['option_id']; // 更新选项票数 $sql = "UPDATE vote_option SET option_count = option_count + 1 WHERE option_id = $option_id"; if ($conn->query($sql) === TRUE) { echo "投票成功"; } else { echo "投票失败:" . $conn->error; } // 关闭数据库连接(同上) ?>
5. Summary
Through the above steps, we have implemented a simple online voting system. Users can complete the entire voting process by creating voting topics, adding voting options, and performing voting operations. Of course, the above code is just an example, and actual development may require more functions and security to be considered.
I hope this article can be helpful to developers who are new to PHP, and I wish you can develop more interesting and practical voting systems!
The above is the detailed content of How to develop a simple online voting system using PHP. For more information, please follow other related articles on the PHP Chinese website!