How to use PHP to complete a CRUD-based data management system

WBOY
Release: 2023-06-25 10:56:01
Original
1509 people have browsed it

In modern network applications, data management is a crucial step. In order to conveniently manage large amounts of data, it is very common to use a management system based on CRUD (Create, Read, Update, Delete). As a widely used website development language, PHP can also provide a wealth of tools and architecture to support CRUD applications. This article will introduce how to use PHP to build a CRUD-based data management system.

  1. Design database structure

Before starting to write PHP code, we must first determine the data collection to be managed and its corresponding properties. Next, we need to design a corresponding MySQL database model. Here we take the management of the "library" data collection as an example. The table designed for this collection is as follows:

book

id | name | author | price

each A row represents a book and its corresponding attributes, where "id" is the unique primary key used to ensure the uniqueness of each book.

  1. Write connection code

Next, we need to connect to the database and establish communication between the PHP code and MySQL. Here, we use the mysqli extension to connect to the MySQL database. The specific connection code is as follows:

// 定义数据库连接信息
$host = "localhost";
$user = "root";
$password = "password";
$database = "library";

// 建立MySQL连接
$conn = new mysqli($host, $user, $password, $database);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败:" . $conn->connect_error);
}
Copy after login

This code establishes a connection to the database named "library" and checks whether the connection is successful. If the connection fails, an error message is output and the run is terminated.

  1. Writing the Create (Create) code

The Create (Create) operation is used to add data, that is, to add a new piece of data to the data collection. In the "Library" data collection, the create operation can be used to add new books. The following is the implementation of the create operation:

// 获取POST请求中的书名(name)、作者(author)和价格(price)
$name = $_POST["name"];
$author = $_POST["author"];
$price = $_POST["price"];

// 构造SQL语句
$sql = "INSERT INTO book (name, author, price) VALUES ('$name', '$author', '$price')";

// 执行SQL语句
if ($conn->query($sql) === TRUE) {
    echo "书籍添加成功";
} else {
    echo "书籍添加失败:" . $conn->error;
}
Copy after login

This code constructs a SQL statement and performs an insert operation by obtaining the book information in the POST request. If the insertion is successful, "Book Added Successfully" is output; otherwise, an error message is output.

  1. Write read (Read) code

The read (Read) operation is used to query data, that is, to find a certain piece of data or a group of data from the data collection. In the "Library" data collection, read operations can be used to query information about all books. The following is the implementation of the read operation:

// 构造SQL语句
$sql = "SELECT * FROM book";

// 获取结果集
$result = $conn->query($sql);

// 检查结果集是否为空
if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - 书名: " . $row["name"]. " - 作者: " . $row["author"]. " - 价格: " . $row["price"]. "<br>";
    }
} else {
    echo "没有书籍信息";
}
Copy after login

This code constructs a SQL statement to obtain the result set and output the book information in the required format. If the result set is empty, "No book information" is output.

  1. Writing update (Update) code

The update (Update) operation is used to modify data, that is, to modify a certain piece of data in the data collection. In the "Library" data collection, the update operation can be used to modify the information of a certain book. The following is the implementation of the update operation:

// 获取POST请求中的书籍ID(id)、书名(name)、作者(author)和价格(price)
$id = $_POST["id"];
$name = $_POST["name"];
$author = $_POST["author"];
$price = $_POST["price"];

// 构造SQL语句
$sql = "UPDATE book SET name='$name', author='$author', price='$price' WHERE id='$id'";

// 执行SQL语句
if ($conn->query($sql) === TRUE) {
    echo "书籍修改成功";
} else {
    echo "书籍修改失败:" . $conn->error;
}
Copy after login

This code constructs a SQL statement and performs an update operation by obtaining the book information in the POST request. If the update is successful, "Book modification successful" is output; otherwise, an error message is output.

  1. Write delete (Delete) code

The delete (Delete) operation is used to delete data, that is, to delete a certain piece or group of data from the data collection. In the "Library" data collection, the delete operation can be used to delete borrowed books. The following is the implementation of the delete operation:

// 获取POST请求中的书籍ID(id)
$id = $_POST["id"];

// 构造SQL语句
$sql = "DELETE FROM book WHERE id='$id'";

// 执行SQL语句
if ($conn->query($sql) === TRUE) {
    echo "书籍删除成功";
} else {
    echo "书籍删除失败:" . $conn->error;
}
Copy after login

This code constructs a SQL statement and performs the delete operation by obtaining the book ID in the POST request. If the deletion is successful, "Book Deletion Successful" is output; otherwise, an error message is output.

  1. Conclusion

The above are all the steps to build a data management system based on CRUD ideas. Using PHP as a development language, we can easily connect to the MySQL database and perform various operations. Of course, in practical applications, we also need to check and filter input parameters to prevent vulnerabilities such as SQL injection. At the same time, we also need to perform performance optimization, security reinforcement and other work to ensure data security and stable operation of the system.

The above is the detailed content of How to use PHP to complete a CRUD-based data management system. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!