PHP development red and blue voting function tutorial database construction
We need to create two tables, one to store the results of our voting, and one to store the user's voting IP records.
We first create a database, the code is as follows
<?php header("Content-type:text/html;charset=utf-8"); //设置编码 $servername = "localhost"; $username = "root"; $password = "root"; // 创建连接 $conn = mysqli_connect($servername, $username, $password); mysqli_set_charset($conn,'utf8'); //设定字符集 // 检测连接 if (!$conn) { die("连接失败: " . mysqli_connect_error()); } // 创建数据库 $sql = "CREATE DATABASE vote"; if (mysqli_query($conn, $sql)) { echo "数据库创建成功"; } else { echo "数据库创建失败: " . mysqli_error($conn); } mysqli_close($conn); ?>
We created a database named "vote"
Now create the first table 'votes' table to record the total number of votes from both red and blue parties
Field name | id | likes | unlikes |
int | |||
10 | 10 | 10 | |
id | Number of red square votes | Number of blue square votes |
Field name | id | vid | ip |
Field type | int | int | varchar |
Field length | 10 | 10 | 40 |
Field description | id address | Record the id of the votes table | The ip address of the voting party |
The code is as follows
<?php header("Content-type:text/html;charset=utf-8"); //设置编码 $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "vote"; // 创建连接 $conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn,'utf8'); //设定字符集 // 检测连接 if (!$conn) { die("连接失败: " . mysqli_connect_error()); } // 使用 sql 创建数据表 $sql = "CREATE TABLE IF NOT EXISTS `votes_ip` ( `id` int(10) NOT NULL, `vid` int(10) NOT NULL, `ip` varchar(40) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; if (mysqli_query($conn, $sql)) { echo "数据表 votes_ip 创建成功"; } else { echo "创建数据表错误: " . mysqli_error($conn); } mysqli_close($conn); ?>
Now that our database has been created, let’s start making the HTML page
||
<?php
header("Content-type:text/html;charset=utf-8"); //设置编码
$servername = "localhost";
$username = "root";
$password = "root";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
mysqli_set_charset($conn,'utf8'); //设定字符集
// 检测连接
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
// 创建数据库
$sql = "CREATE DATABASE vote";
if (mysqli_query($conn, $sql)) {
echo "数据库创建成功";
} else {
echo "数据库创建失败: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
- Course Recommendations
- Courseware download
-
ElementaryImperial CMS enterprise imitation website tutorial
3048 people are watching -
ElementaryNewbies with zero foundation in WordPress build personal blogs and corporate websites
6743 people are watching -
ElementaryUltimate CMS zero-based website building instruction video
2724 people are watching -
ElementaryFront-end project-Shangyou [HTML/CSS/JS technology comprehensive practice]
3117 people are watching -
IntermediateVue3.0 from 0 to build a universal backend management system project practice
5351 people are watching -
ElementaryZero-based front-end course [Vue advanced learning and practical application]
2821 people are watching -
ElementaryWEB front-end tutorial [HTML5+CSS3+JS]
3506 people are watching -
ElementaryQuick introduction to apipost
2161 people are watching -
IntermediateVue3+TypeScript practical tutorial-enterprise-level project practice
3208 people are watching -
ElementaryLet's briefly talk about starting a business in PHP
17423 people are watching -
IntermediateVUE e-commerce project (front-end & back-end dual project actual combat)
3828 people are watching -
ElementaryApipost practical application [api, interface, automated testing, mock]
2265 people are watching
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~
Students who have watched this course are also learning
- Let's briefly talk about starting a business in PHP
- Quick introduction to web front-end development
- Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
- Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
- Login verification and classic message board
- Computer network knowledge collection
- Quick Start Node.JS Full Version
- The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
- Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
- About us Disclaimer Sitemap
- php.cn:Public welfare online PHP training,Help PHP learners grow quickly!