Table is one of the most commonly used components in web development, and the deletion function is also one of the common requirements. In php, delete functionality can be easily implemented. Next we will introduce how to use PHP tables to implement deletion operations.
1. Create a database table
First, we need to create a database table to store the data that needs to be deleted. In this example, we created a table named 'userInfo', which contains four fields: id, name, age, and sex. Corresponding database tables can be established according to the actual situation.
CREATE TABLEuserInfo
(id
int(11) NOT NULL AUTO_INCREMENT,name
varchar(50) NOT NULL ,age
int(11) NOT NULL,sex
int(11) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
2. Connect to the database
Connecting to the database is the prerequisite for deleting the PHP table. We can use the mysqli extension to connect to the database.
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
/ /Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
//Detect connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
3. Query the data in the database
Before performing the deletion operation, we need to query the data in the database and display it in the form of a table. Here we use html code to create the table . Use PHP to perform database query operations and output the query results to a table. The code is as follows:
$sql = "SELECT id, name, age, sex FROM userInfo";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows( $result) > 0) {
// 输出数据 while($row = mysqli_fetch_assoc($result)) { echo ""; echo " "; }" . $row["id"]. " "; echo "" . $row["name"]. " "; echo "" . $row["age"]. " "; echo "" . $row["sex"]. " "; echo "删除 "; echo "
} else {
echo "0 结果";
}
mysqli_close($conn);
4. Delete operation
We create a file named delete.php to receive the data returned after clicking the "Delete" link in the form and perform the deletion operation. The code is as follows:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = " myDB";
//Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
//Detect connection
if (! $conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Get the data returned after clicking the "Delete" link from the table and perform the deletion operation
$id = $_GET['id' ];
$sql = "DELETE FROM userInfo WHERE id=".$id;
if (mysqli_query($conn, $sql)) {
echo "删除成功!";
} else {
echo "Error: " . $sql . "
" . mysqli_error($conn);
}
mysqli_close($conn);
?>
5. To achieve the effect
After completing the above steps, we can see that the corresponding data will be deleted from the database by clicking the "Delete" link in the table. The effect achieved is as shown in the figure below.
Summary:
This article introduces how to implement deletion operations through php tables. It should be noted that when deleting operations, you need to query the data first and display the data in the form of a table, and then perform the deletion operation. The style and display method of the table can be based on actual needs Customize. At the same time, in order to prevent malicious deletion, it is recommended to confirm twice during the deletion operation.
The above is the detailed content of How to use php form to implement delete operation. For more information, please follow other related articles on the PHP Chinese website!