Files
luowei/search_73_1.php
2025-01-08 18:48:04 +08:00

74 lines
2.1 KiB
PHP

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>姓名与地址查询</title>
</head>
<body>
<form method="POST" action="search_73_1.php">
<label for="searchName">姓名查询:</label>
<input type="text" id="searchName" name="searchName">
<label for="searchAddress">地址查询:</label>
<input type="text" id="searchAddress" name="searchAddress">
<label for="and">AND</label>
<input type="radio" id="and" name="searchLogic" value="and" checked>
<label for="or">OR</label>
<input type="radio" id="or" name="searchLogic" value="or">
<input type="submit" id="ok" name="ok" value="ok">
</form>
<?php
include_once 'db_config.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 获取用户输入的查询条件
$searchName = $_POST['searchName'] ?? '';
$searchAddress = $_POST['searchAddress'] ?? '';
$searchLogic = $_POST['searchLogic'] ?? 'and';
// 设置字符集
mysqli_set_charset($link, "utf8");
// 动态构建查询语句
$conditions = [];
$params = [];
$types = '';
if (!empty($searchName)) {
$conditions[] = "name = ?";
$params[] = $searchName;
$types .= 's';
}
if (!empty($searchAddress)) {
$conditions[] = "address = ?";
$params[] = $searchAddress;
$types .= 's';
}
if (count($conditions) > 0) {
$sql = "SELECT * FROM user_70 WHERE " . implode(" " . strtoupper($searchLogic) . " ", $conditions);
$stmt = $link->prepare($sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
$rowCount = $result->num_rows;
echo "<p>[" . $rowCount . "]</p>"; // 显示记录数
if ($rowCount > 0) {
while ($row = $result->fetch_assoc()) {
echo "<p>姓名: " . htmlspecialchars($row['name']) . " 地址: " . htmlspecialchars($row['address']) . "</p>";
}
}
$stmt->close();
} else {
// 如果没有提供查询条件则不执行查询
echo "<p>[0]</p>";
}
}
?>