Cách xóa dữ liệu khỏi bảng HTML trong PHP

element from a table. In other words, Table deleteRow() method is used for deleting row(s) at the specified index in the table. index :It is used to specify the position of the row to be deleted.

Làm cách nào để xóa dữ liệu khỏi bảng trong PHP bằng nút?

Xóa dữ liệu khỏi cơ sở dữ liệu MySql bằng PHP .
XÓA TỪ tên_bảng WHERE cột_name=some_value;
$query1 = mysql_query("xóa khỏi nhân viên nơi employee_id=$del", $connection);

Làm cách nào để XÓA nhiều dữ liệu khỏi bảng trong PHP?

Các bước trong Cập nhật/Xóa nhiều hàng trong PHP .
Chọn hàng bằng cách sử dụng đầu vào hộp kiểm
Hiển thị giao diện người dùng biểu mẫu để cập nhật các cột trong bảng
Gửi mảng chi tiết hàng tới PHP
Lặp lại qua mảng chi tiết hàng để áp dụng truy vấn cập nhật/xóa cho từng

Giống như khi bạn chèn bản ghi vào bảng, bạn có thể xóa bản ghi khỏi bảng bằng cách sử dụng câu lệnh SQL

+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    | [email protected] |
|  2 | John       | Rambo     | [email protected]   |
|  3 | Clark      | Kent      | [email protected]   |
|  4 | John       | Carter    | [email protected]  |
|  5 | Harry      | Potter    | [email protected] |
+----+------------+-----------+----------------------+
9. Nó thường được sử dụng kết hợp với mệnh đề
0 để chỉ xóa những bản ghi phù hợp với tiêu chí hoặc điều kiện cụ thể

Cú pháp cơ bản của câu lệnh

+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    | [email protected] |
|  2 | John       | Rambo     | [email protected]   |
|  3 | Clark      | Kent      | [email protected]   |
|  4 | John       | Carter    | [email protected]  |
|  5 | Harry      | Potter    | [email protected] |
+----+------------+-----------+----------------------+
9 có thể được đưa ra với

XÓA TỪ table_name WHERE column_name=some_value

Hãy tạo một truy vấn SQL bằng cách sử dụng câu lệnh

+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    | [email protected] |
|  2 | John       | Rambo     | [email protected]   |
|  3 | Clark      | Kent      | [email protected]   |
|  4 | John       | Carter    | [email protected]  |
|  5 | Harry      | Potter    | [email protected] |
+----+------------+-----------+----------------------+
9 và mệnh đề
0, sau đó chúng ta sẽ thực hiện truy vấn này thông qua việc chuyển nó đến hàm PHP 
0 để xóa các bản ghi bảng. Xem xét bảng người sau bên trong cơ sở dữ liệu demo

+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    | [email protected] |
|  2 | John       | Rambo     | [email protected]   |
|  3 | Clark      | Kent      | [email protected]   |
|  4 | John       | Carter    | [email protected]  |
|  5 | Harry      | Potter    | [email protected] |
+----+------------+-----------+----------------------+

Đoạn mã PHP trong ví dụ sau sẽ xóa các bản ghi của những người đó khỏi bảng người có first_name bằng với John

Ví dụ

PDO hướng đối tượng thủ tục

Tải xuống

____3____4

Sau khi xóa bảng người sẽ trông như thế này

+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    | [email protected] |
|  3 | Clark      | Kent      | [email protected]   |
|  5 | Harry      | Potter    | [email protected] |
+----+------------+-----------+----------------------+

Như bạn có thể thấy các bản ghi đã được xóa thành công khỏi bảng người

Cảnh báo. Mệnh đề

0 trong câu lệnh 
+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    | [email protected] |
|  2 | John       | Rambo     | [email protected]   |
|  3 | Clark      | Kent      | [email protected]   |
|  4 | John       | Carter    | [email protected]  |
|  5 | Harry      | Potter    | [email protected] |
+----+------------+-----------+----------------------+
9 chỉ định bản ghi hoặc các bản ghi sẽ bị xóa. Nếu bạn bỏ qua mệnh đề
0, tất cả hồ sơ sẽ bị xóa

Tóm lược. trong hướng dẫn này, bạn sẽ học cách xóa dữ liệu khỏi bảng cơ sở dữ liệu MySQL bằng cách sử dụng PHP PDO

Chúng tôi sẽ sử dụng bảng

7 trong cơ sở dữ liệu mẫu để trình diễn. Trước khi tiếp tục với hướng dẫn này, bạn nên làm theo hướng dẫn tạo bảng PHP MySQL để tạo bảng 
7 và chèn dữ liệu mẫu để thực hành

Xem bảng nhiệm vụ sau

Cách xóa dữ liệu khỏi bảng HTML trong PHP
Cách xóa dữ liệu khỏi bảng HTML trong PHP

Để xóa dữ liệu trong bảng, bạn làm theo các bước sau

  • Kết nối với cơ sở dữ liệu MySQL bằng cách tạo phiên bản mới của đối tượng PDO
  • Xây dựng câu lệnh DELETE để xóa một hàng, nhiều hàng hoặc tất cả các hàng trong bảng. Nếu bạn muốn xóa tất cả các hàng trong một bảng lớn một cách nhanh chóng và hiệu quả hơn, bạn sử dụng câu lệnh TRUNCATE TABLE
  • Thực thi câu lệnh
    +----+------------+-----------+----------------------+
    | id | first_name | last_name | email                |
    +----+------------+-----------+----------------------+
    |  1 | Peter      | Parker    | [email protected] |
    |  2 | John       | Rambo     | [email protected]   |
    |  3 | Clark      | Kent      | [email protected]   |
    |  4 | John       | Carter    | [email protected]  |
    |  5 | Harry      | Potter    | [email protected] |
    +----+------------+-----------+----------------------+
    
    9 bằng cách gọi phương thức
    connect_error);
    }
     
    // Attempt delete query execution
    $sql = "DELETE FROM persons WHERE first_name='John'";
    if($mysqli->query($sql) === true){
        echo "Records were deleted successfully.";
    } else{
        echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
    }
     
    // Close connection
    $mysqli->close();
    ?>
    0  của đối tượng
    connect_error);
    }
     
    // Attempt delete query execution
    $sql = "DELETE FROM persons WHERE first_name='John'";
    if($mysqli->query($sql) === true){
        echo "Records were deleted successfully.";
    } else{
        echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
    }
     
    // Close connection
    $mysqli->close();
    ?>
    1 hoặc phương thức
    connect_error);
    }
     
    // Attempt delete query execution
    $sql = "DELETE FROM persons WHERE first_name='John'";
    if($mysqli->query($sql) === true){
        echo "Records were deleted successfully.";
    } else{
        echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
    }
     
    // Close connection
    $mysqli->close();
    ?>
    2 của đối tượng
    connect_error);
    }
     
    // Attempt delete query execution
    $sql = "DELETE FROM persons WHERE first_name='John'";
    if($mysqli->query($sql) === true){
        echo "Records were deleted successfully.";
    } else{
        echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
    }
     
    // Close connection
    $mysqli->close();
    ?>
    3

PHP MySQL Xóa dữ liệu ví dụ

MySQL PHP. xóa một ví dụ hàng đơn

Để xóa một hàng trong bảng, bạn sử dụng câu lệnh

+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    | [email protected] |
|  2 | John       | Rambo     | [email protected]   |
|  3 | Clark      | Kent      | [email protected]   |
|  4 | John       | Carter    | [email protected]  |
|  5 | Harry      | Potter    | [email protected] |
+----+------------+-----------+----------------------+
9 với mệnh đề WHERE chỉ định hàng nào cần xóa

Tập lệnh sau xóa một hàng có id 2 trong bảng

7

/** * PHP MySQL Delete Data Demo */ class DeleteDataDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * PDO instance * @var PDO */ private $pdo = null; /** * Open a database connection to MySQL */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); } catch (PDOException $e) { die($e->getMessage()); } } /** * Delete a task based on a specified task id * @param int $id * @return bool true on success or false on failure */ public function delete($id) { $sql = 'DELETE FROM tasks WHERE task_id = :task_id'; $q = $this->pdo->prepare($sql); return $q->execute([':task_id' => $id]); } /** * close the database connection */ public function __destruct() { $this->pdo = null; } } $obj = new DeleteDataDemo(); // delete id 2 $obj->delete(2);

Code language: HTML, XML (xml)

Làm thế nào nó hoạt động

Trong phương thức

/** * Delete all rows in the tasks table */ public function deleteAll(){ $sql = 'DELETE FROM tasks'; return $this->pdo->exec($sql); }

Code language: HTML, XML (xml)
2  của lớp

/** * Delete all rows in the tasks table */ public function deleteAll(){ $sql = 'DELETE FROM tasks'; return $this->pdo->exec($sql); }

Code language: HTML, XML (xml)
3, chúng tôi kết nối với cơ sở dữ liệu MySQL bằng cách bắt đầu một phiên bản của lớp
connect_error);
}
 
// Attempt delete query execution
$sql = "DELETE FROM persons WHERE first_name='John'";
if($mysqli->query($sql) === true){
    echo "Records were deleted successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
 
// Close connection
$mysqli->close();
?>
1 và trong phương thức

/** * Delete all rows in the tasks table */ public function deleteAll(){ $sql = 'DELETE FROM tasks'; return $this->pdo->exec($sql); }

Code language: HTML, XML (xml)
5  , chúng tôi đóng kết nối cơ sở dữ liệu

Phương thức

/** * Delete all rows in the tasks table */ public function deleteAll(){ $sql = 'DELETE FROM tasks'; return $this->pdo->exec($sql); }

Code language: HTML, XML (xml)
6  chấp nhận

/** * Delete all rows in the tasks table */ public function deleteAll(){ $sql = 'DELETE FROM tasks'; return $this->pdo->exec($sql); }

Code language: HTML, XML (xml)
7 làm đối số. Trước tiên, chúng tôi gọi phương thức

/** * Delete all rows in the tasks table */ public function deleteAll(){ $sql = 'DELETE FROM tasks'; return $this->pdo->exec($sql); }

Code language: HTML, XML (xml)
8  của đối tượng
connect_error);
}
 
// Attempt delete query execution
$sql = "DELETE FROM persons WHERE first_name='John'";
if($mysqli->query($sql) === true){
    echo "Records were deleted successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
 
// Close connection
$mysqli->close();
?>
1 để chuẩn bị thực thi câu lệnh
+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    | [email protected] |
|  2 | John       | Rambo     | [email protected]   |
|  3 | Clark      | Kent      | [email protected]   |
|  4 | John       | Carter    | [email protected]  |
|  5 | Harry      | Potter    | [email protected] |
+----+------------+-----------+----------------------+
9, sau đó chúng tôi chuyển một mảng chứa các giá trị tương ứng với các phần giữ chỗ đã đặt tên trong câu lệnh
+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    | [email protected] |
|  2 | John       | Rambo     | [email protected]   |
|  3 | Clark      | Kent      | [email protected]   |
|  4 | John       | Carter    | [email protected]  |
|  5 | Harry      | Potter    | [email protected] |
+----+------------+-----------+----------------------+
9 cho phương thức
connect_error);
}
 
// Attempt delete query execution
$sql = "DELETE FROM persons WHERE first_name='John'";
if($mysqli->query($sql) === true){
    echo "Records were deleted successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
 
// Close connection
$mysqli->close();
?>
2 của đối tượng
connect_error);
}
 
// Attempt delete query execution
$sql = "DELETE FROM persons WHERE first_name='John'";
if($mysqli->query($sql) === true){
    echo "Records were deleted successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
 
// Close connection
$mysqli->close();
?>
3

Để xóa tác vụ có id 2, chúng ta tạo một thể hiện của lớp

/** * Delete all rows in the tasks table */ public function deleteAll(){ $sql = 'DELETE FROM tasks'; return $this->pdo->exec($sql); }

Code language: HTML, XML (xml)
3 và gọi phương thức

/** * Delete all rows in the tasks table */ public function deleteAll(){ $sql = 'DELETE FROM tasks'; return $this->pdo->exec($sql); }

Code language: HTML, XML (xml)
6

PHPMySQL. xóa tất cả các hàng trong một bảng ví dụ

Có hai cách để xóa tất cả các hàng trong một bảng

  • Đưa ra câu lệnh
    +----+------------+-----------+----------------------+
    | id | first_name | last_name | email                |
    +----+------------+-----------+----------------------+
    |  1 | Peter      | Parker    | [email protected] |
    |  2 | John       | Rambo     | [email protected]   |
    |  3 | Clark      | Kent      | [email protected]   |
    |  4 | John       | Carter    | [email protected]  |
    |  5 | Harry      | Potter    | [email protected] |
    +----+------------+-----------+----------------------+
    
    56 không có mệnh đề WHERE
  • Đưa ra tuyên bố
    +----+------------+-----------+----------------------+
    | id | first_name | last_name | email                |
    +----+------------+-----------+----------------------+
    |  1 | Peter      | Parker    | [email protected] |
    |  2 | John       | Rambo     | [email protected]   |
    |  3 | Clark      | Kent      | [email protected]   |
    |  4 | John       | Carter    | [email protected]  |
    |  5 | Harry      | Potter    | [email protected] |
    +----+------------+-----------+----------------------+
    
    57

Phương thức

+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    | [email protected] |
|  2 | John       | Rambo     | [email protected]   |
|  3 | Clark      | Kent      | [email protected]   |
|  4 | John       | Carter    | [email protected]  |
|  5 | Harry      | Potter    | [email protected] |
+----+------------+-----------+----------------------+
58 sau đây xóa tất cả các hàng trong bảng
7 bằng cách sử dụng câu lệnh 
+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    | [email protected] |
|  2 | John       | Rambo     | [email protected]   |
|  3 | Clark      | Kent      | [email protected]   |
|  4 | John       | Carter    | [email protected]  |
|  5 | Harry      | Potter    | [email protected] |
+----+------------+-----------+----------------------+
9

/** * Delete all rows in the tasks table */ public function deleteAll(){ $sql = 'DELETE FROM tasks'; return $this->pdo->exec($sql); }

Code language: HTML, XML (xml)

Phương thức

71 sau đây sẽ xóa tất cả các hàng trong bảng 
7 cũng như đặt lại giá trị của cột tự động tăng của nó

Làm cách nào để xóa dữ liệu khỏi bảng bằng PHP?

Dữ liệu có thể bị xóa khỏi các bảng MySQL bằng cách thực thi câu lệnh SQL DELETE thông qua hàm mysql_query của PHP . Dưới đây là một ví dụ đơn giản để xóa các bản ghi vào bảng nhân viên. Để xóa một bản ghi trong bất kỳ bảng nào, cần phải xác định vị trí bản ghi đó bằng cách sử dụng mệnh đề điều kiện.

Làm cách nào để xóa dữ liệu khỏi cơ sở dữ liệu khỏi bảng HTML?

The Table deleteRow() method is used for removing a