Home >Backend Development >PHP Tutorial >Second-hand recycling website developed using PHP supports batch deletion of goods
Using PHP to develop a second-hand recycling website to support batch deletion of goods
With the progress of society and the enhancement of people's environmental awareness, the second-hand goods recycling industry has developed rapidly. In order to meet market demand, many companies have opened second-hand recycling websites to facilitate users to sell and purchase second-hand goods. In such websites, product deletion is a common operation. This article will introduce how to use PHP to develop a second-hand recycling website that supports batch deletion of goods, and provide corresponding code examples.
First, we need to create a database table to store product information. Suppose we have a table named "products" that contains the following fields: product ID (id), product name (name), product price (price), and product description (description).
Next, we need to create a page to display the product list and provide the function of batch deletion. We can develop this page using a mix of HTML and PHP. The following is a sample code:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>二手回收网站商品管理</title> </head> <body> <h1>商品列表</h1> <form action="delete.php" method="post"> <table> <tr> <th></th> <th>ID</th> <th>名称</th> <th>价格</th> <th>描述</th> </tr> <?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); if (!$conn) { die("连接数据库失败:" . mysqli_connect_error()); } // 查询商品列表 $sql = "SELECT * FROM products"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_assoc($result)) { echo "<tr>"; echo "<td><input type="checkbox" name="delete[$row[id]]"></td>"; echo "<td>$row[id]</td>"; echo "<td>$row[name]</td>"; echo "<td>$row[price]</td>"; echo "<td>$row[description]</td>"; echo "</tr>"; } } mysqli_close($conn); ?> </table> <input type="submit" value="批量删除"> </form> </body> </html>
In the above code, we use the database connection function mysqli_connect()
to connect to the database, and use the query statement SELECT * FROM products
to Get the product list. In the HTML table, we use 2213c9627c391f00ef101b1592023bcb
to allow users to select items to be deleted.
Next, we need to create the delete.php
page that handles the delete operation. The following is sample code:
<?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); if (!$conn) { die("连接数据库失败:" . mysqli_connect_error()); } // 处理删除操作 if ($_SERVER["REQUEST_METHOD"] == "POST") { if (isset($_POST["delete"])) { foreach ($_POST["delete"] as $id => $value) { $sql = "DELETE FROM products WHERE id = '$id'"; mysqli_query($conn, $sql); } } } mysqli_close($conn); header("Location: index.php"); // 删除完成后跳转回商品列表页面 ?>
In the above code, we first connect to the database. Then, we use $_POST["delete"]
to obtain the product ID selected by the user to be deleted, and perform the deletion operation in sequence. Finally, we use the header()
function to redirect the user back to the product list page.
Through the above code example, we have implemented a second-hand recycling website that supports batch deletion of products. Users can select the products to be deleted on the page. After clicking the "Batch Delete" button, the system will delete the corresponding products according to the user's selection and jump back to the product list page. This function can improve user experience, reduce user operation time, and improve background management efficiency.
Of course, this is just a simple example. A real second-hand recycling website may also include other functions, such as user registration, user login, product release, etc. Developers can expand and optimize according to actual needs.
In short, the second-hand recycling website developed using PHP supports batch deletion of goods, which can meet the needs of users and improve the usability and management efficiency of the website. We hope that the above code examples can provide developers with some reference and help in actual projects.
The above is the detailed content of Second-hand recycling website developed using PHP supports batch deletion of goods. For more information, please follow other related articles on the PHP Chinese website!