Database construction for PHP development login page

Database construction for login page


login.jpg

As we have said in the previous chapter, users The name and password must be stored in the data, so these two fields are essential. We name the username field "username" and the password "password".


Database creation

We can create a database through the mysql knowledge we have learned. This chapter uses our PHP Code to create our database

<?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 login";
if (mysqli_query($conn, $sql)) {
    echo "数据库创建成功";
} else {
    echo "数据库创建失败: " . mysqli_error($conn);
}
mysqli_close($conn);
?>

The above code creates a database named login


Create data table

The data table is named: user

Field nameidusernamepassword
Field typeINT
VARCHAR
VARCHAR
Field length63030
Field descriptionUser’s idUsername password

The data table and field creation code is as follows


<?php
header("Content-type:text/html;charset=utf-8");    //设置编码
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "login";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
 mysqli_set_charset($conn,'utf8'); //设定字符集 
// 检测连接
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}
// 使用 sql 创建数据表
$sql = "CREATE TABLE user (
 id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 username VARCHAR(30) NOT NULL,
 password VARCHAR(30) NOT NULL
 );";
if (mysqli_query($conn, $sql)) {
    echo "数据表 user 创建成功";
} else {
    echo "创建数据表错误: " . mysqli_error($conn);
}
mysqli_close($conn);
?>

The above code creates a database named "user", which contains "id", "username" and "password" Field

Let’s open phpmyadmin and take a look

12.jpg


. We can see that our database has been set up. Of course, this is just the simplest and basic one. After setting up our database, we can create our HTML display page



Continuing Learning
||
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>页面登录</title> </head> <body> <h2>数据库搭建</h2> </body> </html>
submitReset Code