Home > Backend Development > PHP Tutorial > How to develop a simple online voting function using PHP

How to develop a simple online voting function using PHP

WBOY
Release: 2023-09-20 15:14:02
Original
842 people have browsed it

How to develop a simple online voting function using PHP

How to use PHP to develop a simple online voting function

Introduction

Online voting is one of the most common functions in the Internet era, which can help organizations or individuals to quickly and easily collect opinions and choices from large numbers of participants. In this article, we will introduce how to develop a simple online voting function using PHP and provide specific code examples.

Installation and Configuration

Before we begin, we need to make sure that we have installed PHP and a web server suitable for a development environment (such as Apache). If PHP is not installed, you can download the latest PHP version from the official website and follow the installation guide to install it.

Create database

First, we need to create a database to store voting-related data. Execute the following SQL statements in the MySQL database to create a database named "poll" and a data table named "options":

CREATE DATABASE poll;

USE poll;

CREATE TABLE options (
    id INT PRIMARY KEY AUTO_INCREMENT,
    option_text VARCHAR(255),
    vote_count INT DEFAULT 0
);
Copy after login

Writing PHP code

Next, we will Start writing PHP code to implement the voting function. First, we create a file called "index.php". In this file, we add the following code:

<?php
// 引入数据库配置文件
require_once 'config.php';

// 获取投票选项
$options = $db->query("SELECT * FROM options")->fetchAll(PDO::FETCH_ASSOC);

// 处理投票逻辑
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 获取用户选择的选项ID
    $optionId = $_POST['option'];

    // 更新选项的投票数
    $db->query("UPDATE options SET vote_count = vote_count + 1 WHERE id = '$optionId'");
}

// 显示投票表单
?>
<!DOCTYPE html>
<html>
<head>
    <title>在线投票</title>
</head>
<body>
<h1>在线投票</h1>
<form method="post" action="">
    <?php foreach ($options as $option): ?>
        <input type="radio" name="option" value="<?php echo $option['id']; ?>"> <?php echo $option['option_text']; ?><br>
    <?php endforeach; ?>
    <button type="submit">投票</button>
</form>
</body>
</html>
Copy after login

In the above code, we first introduce a database configuration file named "config.php" and get all the voting options. We then check if the user submitted the voting form, and upon submission, we get the option ID selected by the user and update the number of votes for that option. Finally, we display a voting form for the user to select and submit their vote.

Next, we need to create a database configuration file named "config.php", the code is as follows:

<?php
$db_host = 'localhost';
$db_name = 'poll';
$db_user = 'root';
$db_pass = '';
$db = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Copy after login

In the above code, we define the relevant information of the database connection, and Use PDO objects to connect to the database. Here we use the MySQL database. If you use other databases, you need to change the database connection parameters according to the situation.

Testing and running

After completing the above code, we only need to upload the two files "index.php" and "config.php" to the root directory of the web server, and then access the web page. It's time to view and test the online voting feature.

Summary

In this article, we introduced in detail how to use PHP to develop a simple online voting function and provided specific code examples. Through this example, you can learn how to use PHP to connect to the database and implement voting forms and voting logic. Hope this article is helpful to you.

The above is the detailed content of How to develop a simple online voting function using PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template