php实现删除查找
删除和查找功能在PHP中的实现
数据库连接配置
在实现删除和查找功能前,需要先建立数据库连接。创建一个config.php文件存放数据库配置信息:
<?php
$host = 'localhost';
$dbname = 'your_database';
$username = 'your_username';
$password = 'your_password';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
实现删除功能
删除功能通常需要根据ID或其他唯一标识符来删除记录:

<?php
require 'config.php';
if(isset($_GET['id'])) {
$id = $_GET['id'];
$sql = "DELETE FROM your_table WHERE id = :id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $id);
if($stmt->execute()) {
echo "Record deleted successfully";
} else {
echo "Error deleting record";
}
}
?>
实现查找功能
查找功能可以根据不同条件查询数据:
<?php
require 'config.php';
if(isset($_GET['search'])) {
$search = $_GET['search'];
$sql = "SELECT * FROM your_table WHERE name LIKE :search OR email LIKE :search";
$stmt = $conn->prepare($sql);
$stmt->bindValue(':search', '%'.$search.'%');
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(count($results) > 0) {
foreach($results as $row) {
echo "ID: ".$row['id']." - Name: ".$row['name']." - Email: ".$row['email']."<br>";
}
} else {
echo "No results found";
}
}
?>
安全注意事项
使用预处理语句防止SQL注入攻击。所有用户输入都应该经过验证和过滤:

$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if($id === false) {
die('Invalid ID');
}
完整示例页面
创建一个包含搜索和删除功能的完整HTML页面:
<?php
require 'config.php';
// 处理删除请求
if(isset($_GET['delete_id'])) {
$id = $_GET['delete_id'];
$sql = "DELETE FROM your_table WHERE id = :id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->execute();
}
// 处理搜索请求
$results = [];
if(isset($_GET['search'])) {
$search = $_GET['search'];
$sql = "SELECT * FROM your_table WHERE name LIKE :search";
$stmt = $conn->prepare($sql);
$stmt->bindValue(':search', '%'.$search.'%');
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Search and Delete</title>
</head>
<body>
<h1>Search Records</h1>
<form method="GET">
<input type="text" name="search" placeholder="Search...">
<button type="submit">Search</button>
</form>
<?php if(!empty($results)): ?>
<h2>Results</h2>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
<?php foreach($results as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['email']; ?></td>
<td>
<a href="?delete_id=<?php echo $row['id']; ?>" onclick="return confirm('Are you sure?')">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php elseif(isset($_GET['search'])): ?>
<p>No results found.</p>
<?php endif; ?>
</body>
</html>
使用AJAX实现无刷新操作
如果需要更流畅的用户体验,可以使用AJAX来实现无刷新搜索和删除:
// 搜索功能
function searchRecords() {
var search = document.getElementById('search').value;
var xhr = new XMLHttpRequest();
xhr.open('GET', 'search.php?search=' + search, true);
xhr.onload = function() {
document.getElementById('results').innerHTML = this.responseText;
};
xhr.send();
}
// 删除功能
function deleteRecord(id) {
if(confirm('Are you sure?')) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'delete.php?id=' + id, true);
xhr.onload = function() {
searchRecords(); // 刷新结果
};
xhr.send();
}
}






