How to delete data from MySQL table using PHP?

PHPz
Release: 2024-06-05 12:40:57
Original
331 people have browsed it

PHP provides the following methods to delete data in MySQL tables: DELETE statement: used to delete rows matching conditions from the table. TRUNCATE TABLE statement: used to clear all data in the table, including auto-incremented IDs. Practical example: You can delete users from the database using HTML forms and PHP code. The form submits the user ID, and the PHP code uses a DELETE statement to delete the record matching the ID from the users table.

如何使用 PHP 删除 MySQL 表中的数据?

How to delete data from MySQL tables using PHP

PHP provides a variety of methods to delete data from MySQL tables. This tutorial explains the most common methods and provides a practical example.

Method 1: Use the DELETE statement

The easiest way is to use the DELETE statement. It is used to delete rows matching specific conditions from the table. The syntax is as follows:

DELETE FROM table_name
WHERE condition;
Copy after login

For example, to delete users with id as 5 from the users table:

$sql = "DELETE FROM users WHERE id = 5";
$conn->query($sql);
Copy after login

Method 2: Use truncate table

The TRUNCATE TABLE statement is used to delete all data in the table, including auto-incrementing IDs. The syntax is as follows:

TRUNCATE TABLE table_name;
Copy after login

For example, to clear the users table:

$sql = "TRUNCATE TABLE users";
$conn->query($sql);
Copy after login

Practical case

Consider the following HTML form:

<form action="delete.php" method="post">
  <input type="text" name="id">
  <input type="submit" value="Delete">
</form>
Copy after login

In the delete.php file we can delete the user from the database using the id entered by the user:

<?php
// 建立数据库连接
$conn = new mysqli("localhost", "username", "password", "database");

// 获取用户输入的 id
$id = $_POST['id'];

// 准备删除语句
$sql = "DELETE FROM users WHERE id = ?";

// 准备预处理语句
$stmt = $conn->prepare($sql);

// 绑定参数
$stmt->bind_param("i", $id);

// 执行查询
$stmt->execute();

// 关闭预处理语句和数据库连接
$stmt->close();
$conn->close();

// 重定向到列表页面
header("Location: list.php");
?>
Copy after login

This will remove the user from ## Delete the record with id from the #users table that enters a value for the user.

The above is the detailed content of How to delete data from MySQL table using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!