Use PHP to create a powerful online voting platform
Introduction:
With the development of the Internet, online voting has become a common and convenient way to gather opinions and make decisions. In this article, we will use PHP to create a powerful online voting platform that allows users to easily participate in voting and view the results. We will use a combination of HTML, CSS and PHP to achieve this functionality.
Step 1: Create a database
First, we need to create a database to store voting-related data. We can use MySQL database to accomplish this task. Create a database named "voting_platform" in MySQL and create two tables in it: one to store voting questions and options, and another to store user voting results.
Create a table of voting questions and options:
CREATE TABLE questions
(
id
int(11) NOT NULL AUTO_INCREMENT,
question
varchar(255) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE options
(
id
int(11) NOT NULL AUTO_INCREMENT,
question_id
int(11) NOT NULL,
option
varchar(255) NOT NULL,
PRIMARY KEY (id
),
FOREIGN KEY (question_id
) REFERENCES questions
(id
) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Create user voting results table:
CREATE TABLE votes
(
id
int(11) NOT NULL AUTO_INCREMENT,
question_id
int(11) NOT NULL,
option_id
int(11) NOT NULL,
PRIMARY KEY (id
),
FOREIGN KEY (question_id
) REFERENCES questions
(id
) ON DELETE CASCADE,
FOREIGN KEY (option_id
) REFERENCES options
(id
) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Step 2: Create the voting platform page
Next, we can start to create the front-end page of the voting platform. We will use HTML and CSS to create a beautiful and user-friendly interface. On the page, we will display the voting questions and options, and provide a voting button for the user to choose. At the same time, we will also display the voting results.
<!DOCTYPE html> <html> <head> <title>在线投票平台</title> <style> /* 样式定义 */ </style> </head> <body> <h1>在线投票平台</h1> <?php // 连接到数据库 $conn = new mysqli('localhost', 'username', 'password', 'voting_platform'); // 检查连接是否成功 if ($conn->connect_error) { die("连接数据库失败: " . $conn->connect_error); } // 查询投票问题和选项 $sql = "SELECT id, question FROM questions"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 输出投票问题 while($row = $result->fetch_assoc()) { echo "<h2>" . $row["question"] . "</h2>"; // 查询投票选项 $sql = "SELECT id, option FROM options WHERE question_id=" . $row["id"]; $options = $conn->query($sql); if ($options->num_rows > 0) { // 输出投票选项 while($option = $options->fetch_assoc()) { echo "<input type='radio' name='option_" . $row["id"] . "' value='" . $option["id"] . "'>" . $option["option"] . "<br>"; } } echo "<button onclick='vote(" . $row["id"] . ")'>投票</button>"; } } // 关闭数据库连接 $conn->close(); ?> <h2>投票结果</h2> <?php // 连接到数据库 $conn = new mysqli('localhost', 'username', 'password', 'voting_platform'); // 检查连接是否成功 if ($conn->connect_error) { die("连接数据库失败: " . $conn->connect_error); } // 查询投票结果 $sql = "SELECT questions.id, questions.question, options.option, COUNT(votes.id) as count FROM questions JOIN options ON questions.id = options.question_id LEFT JOIN votes ON options.id = votes.option_id GROUP BY options.id"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 输出投票结果 while($row = $result->fetch_assoc()) { echo "<p>" . $row["question"] . "<br>"; echo $row["option"] . ": " . $row["count"] . " 票</p>"; } } // 关闭数据库连接 $conn->close(); ?> <script> // 使用AJAX发送投票请求 function vote(questionId) { var options = document.getElementsByName('option_' + questionId); var selectedOptionId; for (var i = 0; i < options.length; i++) { if (options[i].checked) { selectedOptionId = options[i].value; break; } } if (selectedOptionId) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.status === 200) { alert("投票成功!"); location.reload(); // 刷新页面以更新投票结果 } else { alert("投票失败!"); } } }; xhr.open('POST', 'vote.php', true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send('question_id=' + questionId + '&option_id=' + selectedOptionId); } else { alert("请选择一个选项进行投票!"); } } </script> </body> </html>
Step 3: Process the voting request
Create a PHP file named "vote.php" to process the voting request and save the results to the database.
<?php // 获取问题ID和选项ID $questionId = $_POST['question_id']; $optionId = $_POST['option_id']; // 连接到数据库 $conn = new mysqli('localhost', 'username', 'password', 'voting_platform'); // 检查连接是否成功 if ($conn->connect_error) { die("连接数据库失败: " . $conn->connect_error); } // 插入投票结果到数据库 $sql = "INSERT INTO votes (question_id, option_id) VALUES ($questionId, $optionId)"; if ($conn->query($sql) === TRUE) { echo "投票成功!"; } else { echo "投票失败!"; } // 关闭数据库连接 $conn->close(); ?>
Conclusion:
Through the above steps, we successfully created a powerful online voting platform. Users can select questions and corresponding options to vote, and can also view the voting results. By using PHP and a database, we implemented a simple yet powerful voting platform, providing a convenient and efficient tool for decision-making and opinion collection.
The above is the detailed content of Create a powerful online voting platform using PHP. For more information, please follow other related articles on the PHP Chinese website!