Home > Backend Development > PHP Tutorial > How to implement an online bulletin board using PHP

How to implement an online bulletin board using PHP

王林
Release: 2023-06-27 18:36:01
Original
1419 people have browsed it

With the continuous popularity of the Internet, more and more people are beginning to use the Internet to obtain information. In this context, the bulletin board on the website has become an important information transmission channel. In this article, we will introduce how to use PHP to implement an online bulletin board.

1. Set up a PHP environment

First, we need to set up a PHP environment locally. Generally speaking, we can download and install the AMP suite (Apache MySQL PHP) or the XAMPP suite (XAMPP = Cross-platform (X) Apache MySQL PHP Perl). In this way, we can build a PHP environment locally.

2. Create a database

In order to implement an online bulletin board, we need to create a database to store bulletin information. In MySQL, we can use the following statement to create a database:

CREATE DATABASE bulletin_board;

Then, we can create a table for the database to store bulletins information. The structure of the table is as follows:

CREATE TABLE bulletin (
id int(11) NOT NULL AUTO_INCREMENT,
title varchar (255) NOT NULL,
content text NOT NULL,
time datetime NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

This table contains four fields, namely id, title, content and time. Among them, id is the unique identifier of the announcement, title and content are the title and content of the announcement respectively, and time is the release time of the announcement.

3. Implement the add announcement function

After creating the database, we can start writing PHP code. First, we need to implement the function of adding announcements. The process of adding announcements can be divided into two steps: first, we need to implement the form page for adding announcements; then, we need to implement the function of submitting the form and save the announcement information to the database.

1. The form page for adding announcements

The form page for adding announcements contains a form for entering the title and content of the announcement. The code is as follows:



<title>Add Bulletin</title>
Copy after login


<h1>Add Bulletin</h1>
<form action="add_bulletin.php" method="post">
    <label for="title">Title:</label>
    <input type="text" id="title" name="title" required><br>
    <label for="content">Content:</label>
    <textarea id="content" name="content" rows="10" required></textarea><br>
    <input type="submit" value="Submit">
</form>
Copy after login


In this page, we use a

element and set the action and method attributes. The action attribute specifies the name of the script file that processes form data, and the method attribute specifies the method of data submission.

2. Function of submitting the form

After submitting the form, we need to save the announcement information to the database. The specific implementation method is as follows:

//Connect to the database
$host = 'localhost';
$user = 'root';
$password = '123456 ';
$database = 'bulletin_board';
$conn = mysqli_connect($host, $user, $password, $database);
if (!$conn) {

die('连接失败: ' . mysqli_connect_error());
Copy after login
Copy after login

}

// Process form data
$title = $_POST['title'];
$content = $_POST['content'];
$time = date('Y-m-d H:i:s');

// Insert bulletin
$sql = "INSERT INTO bulletin (title, content, time) VALUES ('$title', '$content', '$time ')";
if (mysqli_query($conn, $sql)) {

echo '添加公告成功!';
Copy after login

} else {

echo '添加公告失败!';
Copy after login

}

// Close the connection
mysqli_close($conn);
?>

In this code, we first connect to the database. Then, get the form data passed in the POST request, which is the title, content, and time of the announcement. Finally, we use the INSERT statement to insert the announcement information into the database.

4. Implement the function of displaying announcements

After adding the announcement, we need to implement the function of displaying announcements. The specific implementation method is as follows:

//Connect to the database
$host = 'localhost';
$user = 'root';
$password = '123456 ';
$database = 'bulletin_board';
$conn = mysqli_connect($host, $user, $password, $database);
if (!$conn) {

die('连接失败: ' . mysqli_connect_error());
Copy after login
Copy after login

}

// Query announcement
$sql = "SELECT * FROM bulletin ORDER BY time DESC";
$result = mysqli_query($conn, $sql);

/ / Output announcement
while ($row = mysqli_fetch_assoc($result)) {

echo '<h2>' . $row['title'] . '</h2>';
echo '<p>' . $row['content'] . '</p>';
echo '<p>' . $row['time'] . '</p>';
echo '<hr>';
Copy after login

}

// Close the connection
mysqli_close($conn);
?> ;

In this code, we first connect to the database. Then, use the SELECT statement to query the announcement information and sort it in reverse chronological order. Finally, we use a while loop to traverse the query results and output the title, content and release time of each announcement.

5. Summary

This article introduces how to use PHP to implement an online bulletin board. We learned how to create databases and tables, and how to add announcements and display announcements. I hope this article can help everyone better understand the application of PHP.

The above is the detailed content of How to implement an online bulletin board using PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template