How to delete a row from a database table in PHP?
In PHP, you can use the DELETE command in SQL to delete a specific row in a database table. Here is an example code:
<?php
// 连接数据库
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接数据库失败: " . $conn->connect_error);
}
// 删除表单中某一行内容
$id = 1; // 要删除的行的ID
$sql = "DELETE FROM 表名 WHERE id = $id";
if ($conn->query($sql) === TRUE) {
echo "删除成功";
} else {
echo "删除失败: " . $conn->error;
}
// 关闭数据库连接
$conn->close();
?>
Please replace “your_username”, “your_password”, and “your_dbname” in the above code with your database username, password, and database name. Replace the table name with the name of the table you want to delete data from, and replace “$id” with the ID of the row you want to delete. After executing the code, the row corresponding to the specified ID will be deleted from the specified database table.