How to delete a record from the database in PHP?

In PHP, you can delete a record from a database using SQL statements. The syntax involves using the DELETE FROM statement, followed by specifying the table and conditions for deletion.

Here is an example code:

<?php
// 数据库连接信息
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 定义要删除的记录的条件
$id = 1;

// 创建删除记录的SQL语句
$sql = "DELETE FROM your_table_name WHERE id = $id";

// 执行SQL语句并检查是否成功
if ($conn->query($sql) === TRUE) {
    echo "记录删除成功";
} else {
    echo "删除记录时出现错误: " . $conn->error;
}

// 关闭连接
$conn->close();
?>

Please note that you need to replace “your_table_name” with the name of your table, and “id” with the condition of the record you want to delete.

bannerAds