PHP Database Deletion: How to Delete Records with PHP
In PHP, you can use SQL statements to achieve the delete function. Here is a simple example:
<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 获取要删除的数据的id
$id = $_GET['id'];
// 使用SQL语句删除数据
$sql = "DELETE FROM tablename WHERE id='$id'";
if ($conn->query($sql) === TRUE) {
echo "数据删除成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
In the example above, we start by connecting to the database, then retrieve the id of the data to be deleted, and use the DELETE statement to remove that data. Finally, we check if the operation was successful and close the database connection. Keep in mind that this is just a simple example, so you may need to modify the code accordingly based on your specific situation.