search
HomeBackend DevelopmentPHP ProblemHow to delete a row on click in php

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=, which means that the id of the deleted data is passed to the processing script named delete.php.

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!

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
How to Implement message queues (RabbitMQ, Redis) in PHP?How to Implement message queues (RabbitMQ, Redis) in PHP?Mar 10, 2025 pm 06:15 PM

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

What Are the Latest PHP Coding Standards and Best Practices?What Are the Latest PHP Coding Standards and Best Practices?Mar 10, 2025 pm 06:16 PM

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

How to Use Asynchronous Tasks in PHP for Non-Blocking Operations?How to Use Asynchronous Tasks in PHP for Non-Blocking Operations?Mar 10, 2025 pm 04:21 PM

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

How to Use Reflection to Analyze and Manipulate PHP Code?How to Use Reflection to Analyze and Manipulate PHP Code?Mar 10, 2025 pm 06:12 PM

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

How Do I Work with PHP Extensions and PECL?How Do I Work with PHP Extensions and PECL?Mar 10, 2025 pm 06:12 PM

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 JIT (Just-In-Time) Compilation: How it improves performance.PHP 8 JIT (Just-In-Time) Compilation: How it improves performance.Mar 25, 2025 am 10:37 AM

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

How to Use Memory Optimization Techniques in PHP?How to Use Memory Optimization Techniques in PHP?Mar 10, 2025 pm 04:23 PM

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

How Do I Stay Up-to-Date with the PHP Ecosystem and Community?How Do I Stay Up-to-Date with the PHP Ecosystem and Community?Mar 10, 2025 pm 06:16 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

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

Dreamweaver Mac version

Visual web development tools