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.
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}'
);
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);
}
// 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>"; }
} else {
echo "暂无可用的调查。";
}
$ conn->close();
?>
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);
}
// 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; }
} else {
echo "调查不存在。";
}
$conn->close();
?> ;
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);
}
// 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>"; } }
} else {
echo "暂无可用的调查。";
}
$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!