Home >Backend Development >PHP Problem >How 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 href='delete.php?id=" . $row["id"] . "'>删除</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!