As time goes by, web development technology continues to develop and advance, and PHP, as a popular back-end development language, plays a pivotal role in web development. In web development, it is often necessary to write some code that interacts with the database. One of the common requirements is to delete data in a table. This article will introduce how to use PHP to realize the function of clicking to delete a row of data.
First, we need to create a table in the database to store data. Assume that the table we want to delete now includes three fields: id, name, and address. We can create a table named test with the following structure:
CREATE TABLE test (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
address VARCHAR(50)
);
Next, we can write code in PHP to connect to the database and retrieve it from Query the data that needs to be deleted from the table. Mysqli is used to connect to the database here. The code is as follows:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "test_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check whether the connection is successful
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// Query the data in the table
$sql = "SELECT * FROM test";
$result = $conn->query($sql);
// Output table data to HTML page
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) { echo "<tr> <td>" . $row["id"]. "</td> <td>" . $row["name"]. "</td> <td>" . $row["address"]. "</td> <td><a>删除</a></td> </tr>"; }
} else {
echo "0 结果";
}
$conn->close();
?>
The above code queries all the data in the test table, and then Output data to HTML page. Notice that in the last line, we added a hyperlink to implement the click-to-delete function.
The URL of the hyperlink is delete.php?id=
Now we need to write the delete.php script to implement the click delete function. The code is as follows:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = " test_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check whether the connection is successful
if ($ conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// Get the id of deleted data
$id = $_GET["id"];
// Delete data
$sql = "DELETE FROM test WHERE id=$id";
if ($conn->query($sql) === TRUE) {
echo "删除成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
In the above code, first get the requested content from the URL The ID of the deleted data, and then use the DELETE statement to delete the corresponding data from the table.
In this article, we introduce how to use PHP to realize the function of clicking to delete a row of data. By connecting to the database to query data and adding a hyperlink to the HTML page, the deletion operation of a row of data is realized. At the same time, we also mentioned considerations such as security and error handling.
Web development is an evolving technology, and we need to constantly learn and update our knowledge. Hope this article can be helpful to you.
The above is the detailed content of How to delete a row on click in php. For more information, please follow other related articles on the PHP Chinese website!

This article details implementing message queues in PHP using RabbitMQ and Redis. It compares their architectures (AMQP vs. in-memory), features, and reliability mechanisms (confirmations, transactions, persistence). Best practices for design, error

This article examines current PHP coding standards and best practices, focusing on PSR recommendations (PSR-1, PSR-2, PSR-4, PSR-12). It emphasizes improving code readability and maintainability through consistent styling, meaningful naming, and eff

This article explores asynchronous task execution in PHP to enhance web application responsiveness. It details methods like message queues, asynchronous frameworks (ReactPHP, Swoole), and background processes, emphasizing best practices for efficien

This article explains PHP's Reflection API, enabling runtime inspection and manipulation of classes, methods, and properties. It details common use cases (documentation generation, ORMs, dependency injection) and cautions against performance overhea

This article details installing and troubleshooting PHP extensions, focusing on PECL. It covers installation steps (finding, downloading/compiling, enabling, restarting the server), troubleshooting techniques (checking logs, verifying installation,

PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

This article addresses PHP memory optimization. It details techniques like using appropriate data structures, avoiding unnecessary object creation, and employing efficient algorithms. Common memory leak sources (e.g., unclosed connections, global v

This article explores strategies for staying current in the PHP ecosystem. It emphasizes utilizing official channels, community forums, conferences, and open-source contributions. The author highlights best resources for learning new features and a


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools