This repository has been archived on 2025-01-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
luowei_exam/search_73_1_e.php
2025-01-09 11:57:41 +08:00

69 lines
2.0 KiB
PHP

<?php
// 处理表单提交的 POST 请求
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// 数据库配置
$host = "localhost";
$dbname = "exam_20250109_58053";
$dbuser = "user_20250109_58053";
$dbpass = "fFPALeYRQxo32lB";
// 接收表单数据
$sName = trim($_POST["sName"]);
$sAddress = trim($_POST["sAddress"]);
$sLogic = $_POST["sLogic"];
// 创建数据库连接
$conn = new mysqli($host, $dbuser, $dbpass, $dbname);
if ($conn->connect_error) {
die("数据库连接失败: " . $conn->connect_error);
}
// 动态构建 SQL 查询语句
$conditions = [];
if (!empty($sName)) {
$conditions[] = "name LIKE '%" . $conn->real_escape_string($sName) . "%'";
}
if (!empty($sAddress)) {
$conditions[] = "address LIKE '%" . $conn->real_escape_string($sAddress) . "%'";
}
$query = "SELECT * FROM user_70";
if (count($conditions) > 0) {
$query .= " WHERE " . implode(" $sLogic ", $conditions);
}
// 执行查询
$result = $conn->query($query);
if ($result) {
$rowCount = $result->num_rows;
echo "[[$rowCount]]"; // 输出符合条件的记录数
} else {
echo "[[0]]"; // 查询失败
}
// 关闭连接
$conn->close();
} else {
// 显示查询表单
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>多条件组合查询</title>
</head>
<body>
<form action="search_73_1_e.php" method="POST">
<label for="sName">姓名:</label>
<input type="text" id="sName" name="sName"><br>
<label for="sAddress">地址:</label>
<input type="text" id="sAddress" name="sAddress"><br>
<label>连接条件:</label>
<input type="radio" id="and" name="sLogic" value="and" checked> AND
<input type="radio" id="or" name="sLogic" value="or"> OR<br>
<button type="submit" id="ok" name="ok" value="ok">确定</button>
</form>
</body>
</html>
<?php
}