Design and practice of anonymous voting system implemented in PHP
Abstract:
In recent years, with the development of the Internet, the voting system has gradually entered the Internet age. In order to ensure the fairness and anonymity of voting, it is particularly important to design a safe and reliable voting system. This article uses PHP as the development language to introduce a design plan for implementing an anonymous voting system, and attaches code examples.
(1) User registration and login:
First, we create a users table to store user information, including fields such as user name and password.
CREATE TABLE users
(
id
int(11) NOT NULL AUTO_INCREMENT,
username
varchar(50) NOT NULL ,
password
varchar(50) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Then, we can use PHP code to implement user registration and login functions. The following is a sample code:
User registration:
if($_POST['username'] && $_POST['password']){
$username = $_POST['username']; $password = md5($_POST['password']); // 存储到数据库 // ... echo "注册成功!";
}
?>
User login:
if($_POST['username'] && $_POST['password']) {
$username = $_POST['username']; $password = md5($_POST['password']); // 校验用户名和密码 // ... if(用户名和密码正确){ echo "登录成功!"; } else { echo "用户名或密码错误!"; }
}
?>
(2) Initiate voting:
We create a votes table to store voting information, including voting name, initiating user and voting options, etc. field.
CREATE TABLE votes
(
id
int(11) NOT NULL AUTO_INCREMENT,
title
varchar(255) NOT NULL ,
user_id
int(11) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE options
(
id
int(11) NOT NULL AUTO_INCREMENT,
vote_id
int(11) NOT NULL,
content
varchar(255) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Users can submit votes through the form Information, the following is the sample code:
if($_POST['title'] && $_POST['options']){
$title = $_POST['title']; $options = $_POST['options']; // 存储到数据库 // ... echo "投票创建成功!";
}
?>
(3) Voting:
Users can select the polls they are interested in and vote. Here is the sample code:
if($_POST['vote_id'] && $_POST['option_id']){
$vote_id = $_POST['vote_id']; $option_id = $_POST['option_id']; // 检查用户是否已经投过票 // ... // 更新选项的票数 // ... echo "投票成功!";
}
?> ;
(4) Result display:
We can display the voting results by querying the database. The following is sample code:
if($_GET['vote_id']){
$vote_id = $_GET['vote_id']; // 查询选项的票数 // ... // 展示投票结果 // ...
}
?>
The above is the detailed content of Design and practice of anonymous voting system implemented in PHP. For more information, please follow other related articles on the PHP Chinese website!