Home  >  Article  >  Backend Development  >  How to implement addition, deletion, modification and query of news in php

How to implement addition, deletion, modification and query of news in php

PHPz
PHPzOriginal
2023-04-19 11:35:54747browse

1. Foreword
With the continuous development of the Internet, news has become an important channel for people to obtain information. For news media, how to efficiently manage and display news has become an urgent problem to be solved. This article will be based on PHP technology and explain how to implement the function of adding, deleting, modifying and checking news.

2. Environment setup
First, we need to prepare a server environment that integrates Apache, PHP and MySQL. For Windows users, you can choose to install a development kit such as XAMPP or WAMP. For Linux users, you can install the required software packages through the command line. Finally, enable the Apache and MySQL services by editing the configuration file, and configure the connection to the MySQL database.

3. Database design
Before implementing the news management function, we need to design the news data table structure first. In this article, we will design a news table containing news titles, authors, content and release time. The SQL statement is as follows:

CREATE TABLE news (
id INT(11) NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
author VARCHAR(50) NOT NULL,
content TEXT NOT NULL,
publish_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE= InnoDB DEFAULT CHARSET=utf8;

4. Add news
Before implementing the function of adding news, we need to prepare a form for adding news. The form contains three fields: title, author and content. When submitting the form, we need to write the values ​​of these three fields into the database. The specific code is as follows:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$title = $_POST['title'];
$author = $_POST['author'];
$content = $_POST['content'];

// 连接数据库
$host = 'localhost';
$user = 'root';
$password = '';
$dbname = 'test';
$conn = mysqli_connect($host, $user, $password, $dbname);
if (!$conn) {
    die('连接数据库失败: ' . mysqli_connect_error());
}

// 添加数据
$sql = "INSERT INTO news (title, author, content) VALUES ('$title', '$author', '$content')";
if (mysqli_query($conn, $sql)) {
    echo '添加新闻成功';
} else {
    echo '添加新闻失败: ' . mysqli_error($conn);
}

// 关闭连接
mysqli_close($conn);

}
?>

5. Delete News
The delete news function needs to receive a news ID, and then delete the corresponding record from the database based on this ID. The specific code is as follows:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$id = $_POST['id'];

// 连接数据库
$host = 'localhost';
$user = 'root';
$password = '';
$dbname = 'test';
$conn = mysqli_connect($host, $user, $password, $dbname);
if (!$conn) {
    die('连接数据库失败: ' . mysqli_connect_error());
}

// 删除数据
$sql = "DELETE FROM news WHERE id = $id";
if (mysqli_query($conn, $sql)) {
    echo '删除新闻成功';
} else {
    echo '删除新闻失败: ' . mysqli_error($conn);
}

// 关闭连接
mysqli_close($conn);

}
?>

6. Modify News
The function of modifying news needs to receive a news ID, then read the corresponding record from the database and display it in an edit form. Users can modify the title, author and content of the news in this form. After the edit form is submitted, we need to write the modified data to the database. The specific code is as follows:

if ($_SERVER['REQUEST_METHOD'] == 'GET') {

$id = $_GET['id'];

// 连接数据库
$host = 'localhost';
$user = 'root';
$password = '';
$dbname = 'test';
$conn = mysqli_connect($host, $user, $password, $dbname);
if (!$conn) {
    die('连接数据库失败: ' . mysqli_connect_error());
}

// 查询数据
$sql = "SELECT * FROM news WHERE id = $id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$title = $row['title'];
$author = $row['author'];
$content = $row['content'];

// 关闭连接
mysqli_close($conn);

} elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {

$id = $_POST['id'];
$title = $_POST['title'];
$author = $_POST['author'];
$content = $_POST['content'];

// 连接数据库
$host = 'localhost';
$user = 'root';
$password = '';
$dbname = 'test';
$conn = mysqli_connect($host, $user, $password, $dbname);
if (!$conn) {
    die('连接数据库失败: ' . mysqli_connect_error());
}

// 修改数据
$sql = "UPDATE news SET title='$title', author='$author', content='$content' WHERE id=$id";
if (mysqli_query($conn, $sql)) {
    echo '修改新闻成功';
} else {
    echo '修改新闻失败: ' . mysqli_error($conn);
}

// 关闭连接
mysqli_close($conn);

}
?>

7. Query News
The query news function needs to read all news from the database and display it in in a list. Each news item can be edited and deleted from this list. The specific code is as follows:

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

die('连接数据库失败: ' . mysqli_connect_error());

}

//Query data
$sql = "SELECT * FROM news";
$result = mysqli_query($conn, $sql);

//Output data
echo '

';
echo '';
while ($row = mysqli_fetch_assoc($result)) {

echo '<tr>';
echo '<td>' . htmlspecialchars($row['title']) . '</td>';
echo '<td>' . htmlspecialchars($row['author']) . '</td>';
echo '<td>' . htmlspecialchars($row['content']) . '</td>';
echo '<td>' . htmlspecialchars($row['publish_time']) . '</td>';
echo '<td><a href="edit.php?id=&#39; . $row[&#39;id&#39;] . &#39;">编辑</a></td>';
echo '<td><form method="post" action="delete.php"><input type="hidden" name="id" value="&#39; . $row[&#39;id&#39;] . &#39;"><button>删除</button></form></td>';
echo '</tr>';

}
echo '

TitleAuthorContent
';

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

eight , Summary
Through the above steps, we successfully implemented the function of adding, deleting, modifying and checking news. In this way, news media can easily manage and display their news content and provide readers with better information services. It is worth noting that the code in this article is for reference only and needs to be modified according to specific needs in actual projects.

The above is the detailed content of How to implement addition, deletion, modification and query of news in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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