以下是我的代码,从上一个页面中点击超链接获取了数据库中的链接信息,转到本页面,从而得到了URL参数$name,然后我需要在本页面执行查询功能,查询方式是2选一,如下代码所示,同时,还要能够事先筛选出含有$name参数数据的m_name字段对应的数据;可是,在执行查询的时候,$name参数始终是处于空值状态,请各位帮我修改一下代码!谢谢!
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once './connections/games.php';
// 从 URL 中获取 name 参数
$name = isset($_GET['name']) ? $_GET['name'] : '';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="./css/styles.css" rel="stylesheet" type="text/css">
<title>成绩查询 | Search</title>
</head>
<body>
<div><a style="font-weight: bolder;font-size: 20px;">成绩查询 | Search</a><a style="color:yellow">【本结果仅供参考,所有成绩以现场播报和张贴公示为准!】</a></div>
<div class="sl">
<form id="sh-form" method="post" action="sh.php">
<label style="font-size: 16px;font-weight:bold;font-color:#FFF;">查询方式:</label>
<select style="height:26px" name="searchType" id="searchType">
<option value="bib">编号</option>
<option value="name">姓名</option>
</select>
<input style="height:20px; border-radius: 3px; border: 1px solid #2b7acd;" type="text" id="searchInput" name="searchInput" placeholder="请输入" required>
<button type="searchButton" id="searchButton">查询</button>
</form>
</div>
<br>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('#searchButton').click(function(event) {
event.preventDefault(); // 阻止表单的默认提交行为
// 获取用户输入
var searchType = $('#searchType').val();
var searchInput = $('#searchInput').val();
// 验证查询类型
if (!searchType || !searchInput) {
alert('请填写正确的查询方式及对应的信息!');
return;
}
// 发送Ajax请求到服务器
$.ajax({
url: 'sh.php',
type: 'POST',
data: {
searchType: searchType,
searchInput: searchInput
},
success: function(response) {
// 在成功回调中处理服务器返回的数据
$('#main-content').html(response);
},
error: function(xhr, status, error) {
// 处理错误情况
alert('查询失败: ' + error);
}
});
});
});
</script>
<?php
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// 获取用户输入
$searchType = $_POST['searchType'];
$searchInput = $_POST['searchInput'];
// 初始化SQL语句,不带WHERE子句
$sql = "SELECT * FROM m_result";
// 根据查询类型构建SQL语句
$params = []; // 用于存储绑定参数的数组
if ($searchType === 'bib') { // 编号查询
$sql .= " WHERE bib = :searchInput";
$params[':searchInput'] = $searchInput;
} elseif ($searchType === 'name') { // 姓名查询
$sql .= " WHERE `name` = :searchInput";
$params[':searchInput'] = $searchInput;
}
$name = isset($_GET['name']) ? $_GET['name'] : '';
echo "组别".$name;// 这里无法获取到$name参数,所以查询不到数据
// 如果$name变量存在且不为空,则添加额外的筛选条件
if (!empty($name)) {
$sql .= " AND `m_name` = :name";
$params[':name'] = $name;
}
// 如果没有任何查询条件,则不执行查询
if (empty($params)) {
echo "查询无效,请提供有效的查询条件!";
exit;
}
// 准备SQL语句
$stmt = $pdo->prepare($sql);
// 绑定参数并执行查询
foreach ($params as $param => $value) {
$stmt->bindParam($param, $value);
}
try {
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 显示查询结果
if (!empty($results)) {
?>
<div>
<table>
<tr align="center" style="background:#FFF;font-weight: bold;">
<th>排名<br>Rank</th>
<th>编号<br>Bib</th>
<th>姓名<br>Name</th>
</tr >
<?php foreach ($results as $row) { ?>
<tr>
<td><?php echo $row['rank'];?></td>
<td><?php echo $row['bib'];?></td>
<td><?php echo $row['name'];?></td>
</tr>
<?php
}
?>
</table>
<?php
} else {
echo "没有找到匹配的结果!";
}
} catch (\PDOException $e) {
die("查询错误: " . $e->getMessage());
}
}
?>
</body>
</html>