PHP development...LOGIN

PHP development small forum tutorial database construction

Create database

We create a database named 'mybbs'

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

Then create our forum section table 'forums'

This table stores the fields of those forums we have published

varchar6200Field descriptionidIntroduction to the forum
Nameidforum_name
forum_description                                                                                                          #Field typeINT
varchar
varchar

datetime

Field length

50
50
Name of the forum
Forum The topicForum creation time

The code is as follows

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

Create our user table and store our users in the "member" table , without which forums and posts are not allowed,

##Field DescriptionUser IDUsername to be filled in for registrationPassword to be filled in for registrationE-mail address to be filled in for registrationRegistration time
Field nameidusername
password email log_time
Field typeINT
varchar
varchar
varchar
datetime
Field length11505050

The creation code is as follows

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


The following creates our post table 'tiopic'

'tiopic 'Stored fields

    ##author Author of the post
  • title The title of the post
  • content The content of the post
  • ##last_post_time The time when the post was published
  • reply_author The person who responded to the post
  • reply The reply content of the post
  • reply_time The time to reply to the post
  • The code is as follows
  • <?php
    header("Content-type:text/html;charset=utf-8");    //设置编码
    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "mybbs";
    // 创建连接
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    mysqli_set_charset($conn,'utf8'); //设定字符集
    // 检测连接
    if (!$conn) {
        die("连接失败: " . mysqli_connect_error());
    }
    // 使用 sql 创建数据表
    $sql = "CREATE TABLE tiopic (
     id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
     `author` varchar(50) NOT NULL,
      `title` varchar(100) NOT NULL,
      `content` text NOT NULL,
      `last_post_time` datetime NOT NULL,
      `reply_author` varchar(50) DEFAULT NULL,
      `reply` text,
      `reply_time` datetime DEFAULT NULL
    );";
    if (mysqli_query($conn, $sql)) {
        echo "数据表 tiopic 创建成功";
    } else {
        echo "创建数据表错误: " . mysqli_error($conn);
    }
    mysqli_close($conn);
    ?>

Tip: This tutorial only demonstrates the simple forum principle, so the content of the posted post and the content of the reply post are put together. A truly complete forum reply requires PHP recursion is used. This tutorial does not use recursion, so when replying to a post, the new content will overwrite the previous content. I hope that learners will create a more complete forum after completing this course



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