Home >Backend Development >PHP Tutorial >PHP voting system simple implementation source code (1/3)_PHP tutorial
This article introduces in detail the implementation principle and implementation code of the voting system. Friends in need can refer to it.
Database design
Design three tables: voting result statistics table (count_voting), voter record table (ip_votes), user table (user)
voting result statistics The table is used to count the final voting records. I have four fields for it: the name of the voted item (SelectName), the label name of the voted item (LabelName) (which plays a role in classification), and the number of votes (CountVotes).
The voter record table is used to register the voter’s IP (IP), geographical location (Location), voting time (VoteTime), and the name of the voted item (SelectName). Then I also add an ID to it.
The user table is mainly used for administrators, including username (name) and password (passwd).
The sql script to generate the table is as follows:
The code is as follows | Copy code | ||||
|
As you can see from the script, I created a trigger that adds 1 to the CountVotes field in the count_voting table when data is inserted into the ip_votes table. The last sentence can also be used to set external related words.
Framework design
The OperatorDB class is used to operate the database, and the OperatorVotingDB class is used for the system-specific set of operations.
Use PDO to operate the database, let me simply encapsulate it:
The code is as follows | Copy code | ||||
"); } } public function __destruct() { $this-> ;pdo = null; } public function exec($sql) { } public function query($sql) { }} |
Encapsulate the database connection information to facilitate subsequent operations.
The code is as follows | Copy code |
public function __construct() /** /** $subsql = "SELECT MAX(to_days(VoteTime)) FROM ip_votes WHERE IP='$ip'"; /** /** /** |
1 2 3