请各位帮我看下代码,解决一个URL的参数传递问题:
1、matches.php页面调取数据库中matches表中的name、link_url字段数据,并形成数据表,下面代码所示:
<?php
// 确保输入是安全的
$year = $_POST['year'] ?? '';
$game = $_POST['game'] ?? '';
// 从记录集中调取出对应信息
$stmt = $pdo->prepare("
SELECT DISTINCT
`year`,
`game`,
`name`,
linkimage,
link_url,
FROM
matches
WHERE
`year` = ? AND
`game` = ?
ORDER BY
`date` ASC
");
$stmt->execute([$year, $game]);
$order = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!--选定之后,显示比赛信息区域 -->
<p></p><div>
<?php
$i = 0; // 添加一个计数器变量
foreach ($order as $row):
// 判断是否是每四个表格的开始,如果是则开始一个新的行
if ($i % 4 == 0) {
echo '<div class="row">'; // 开始新的行
}
// 表格包裹的div,添加table-wrapper类
echo '<div class="table-wrapper">';
?>
<table style="font-family: inherit; color:#FFF; width: 100%; margin-bottom: 10px;">
<tr align="center" style="font-weight:bold; font-size:10px;">
<td style="height:284px;width:210px">
<?php
$imagePath = "./image/link/{$row['linkimage']}.jpg";
// 检查图片是否存在
if (file_exists($imagePath)) {
// 如果图片存在,只显示图片并包裹在超链接中
echo "<a href='mm/index.php?name=" . urlencode($row['name']) . "' target='_blank'>";
echo "<img src='{$imagePath}' alt='{$row['name']}' width='210' height='280'>";
echo "</a>";
} else {
// 如果图片不存在,显示文本名称并包裹在超链接中
echo "<a href='mm/index.php?name=" . urlencode($row['name']) . "' target='_blank'>";
echo htmlspecialchars($row['name']);
echo "</a><br>";
}
?></td>
</tr>
<tr>
<td><?php echo htmlspecialchars($row['date']); ?> · <?php echo htmlspecialchars($row['city']); ?></td>
</tr>
<tr>
<td><?php echo htmlspecialchars($row['station']); ?></td>
</tr>
</table>
<?php
echo '</div>'; // 结束table-wrapper的div
// 判断是否是每四个表格的结束,如果是则结束当前行
if (($i + 1) % 4 == 0) {
echo '</div>'; // 结束row的div
}
$i++; // 计数器加1
endforeach;
// 检查是否还有未闭合的行标签,如果有则闭合它
if ($i % 4 != 0) {
echo '</div>'; // 闭合最后一行的div
}
?>
2、点击name的链接后,页面跳转至mm/index.php,并且保留了$name参数,这一步成功的,代码如下:
<?php
// 获取 URL 中的 name 参数
$name = isset($_GET['name']) ? $_GET['name'] : null;
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>
<?php echo $name;?></title>
<link href="./css/szlhls.css" rel="stylesheet" type="text/css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
// 加载 rc.php 到 <main> 标签
$('#load_rc').click(function(e) {
e.preventDefault();
$('#main-content').load('rc.php', function() {
// rc.php 内容加载完成后的回调函数
});
});
// 加载 sl.php 到 <main> 标签
$('#load_sl').click(function(e) {
e.preventDefault();
$('#main-content').load('sl.php', function() {
// sl.php 内容加载完成后的回调函数
});
});
});
</script>
</head>
<body>
<div class="app">
<header class="header">
<a class="link-logo"><img src="./image/head_bg.jpg" width=90% height=100%></a>
</header>
<div class="container">
<div class="nav">
<button class="button" type="button" id="load_rc">日程 | Schedule</button>
<button class="button" type="button" id="load_sl">顺序 | Start Lists</button>
</div>
<div class="main">
<main id="main-content"><a style="color:#FFF"><?php echo $name;?></a></main>
</div>
</div>
</body>
</html>
同时,index.php中包含了一个
标签,用以调取和显示rc.php和sl.php页面的调取数据,需要将当前页面的$name参数传递给rc.php和sl.php以实现调取和显示.3、rc.php代码如下:在这个页面,通过前面步骤获取到的$name全局变量,以调取数据库中m_date表中name对应的其他数据,并且显示在index.php页面中的标签里。
现在就是$name这个变量无法传递到rc.php页面,数值为空,请帮我检查一下代码,谢谢!
<?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'] : '';
$stmt = $pdo->prepare('SELECT DISTINCT `date` FROM m_date WHERE `name` = :name');
$stmt->bindParam(':name', $name);
$stmt->execute();
$dateList = $stmt->fetchAll(PDO::FETCH_COLUMN);
// 设置默认选中的值
$selectedDate = isset($_POST['date']) ? $_POST['date'] : '';
$selectedApm = isset($_POST['apm']) ? $_POST['apm'] : '';
$selectedPlace = isset($_POST['place']) ? $_POST['place'] : '';
$selectedOrder = isset($_POST['order']) ? $_POST['order'] : '';
// 渲染日期下拉菜单
foreach ($dateList as $date) {}
// 根据日期渲染时段下拉菜单
if (!empty($selectedDate)) {
$apmStmt = $pdo->prepare('SELECT DISTINCT `apm` FROM m_date WHERE `date` = :date');
$apmStmt->bindParam(':date', $selectedDate);
$apmStmt->execute();
$apmList = $apmStmt->fetchAll(PDO::FETCH_COLUMN);
// 根据游戏和年份动态渲染比赛下拉菜单
if (!empty($selectedApm)) {
$placeStmt = $pdo->prepare('SELECT DISTINCT `place` FROM m_date WHERE `date` = :date AND `apm` = :apm');
$placeStmt->bindParam(':date', $selectedDate);
$placeStmt->bindParam(':apm', $selectedApm);
$placeStmt->execute();
$placeList = $placeStmt->fetchAll(PDO::FETCH_COLUMN);
// 根据游戏和年份动态渲染比赛下拉菜单
if (!empty($selectedPlace)) {
$orderStmt = $pdo->prepare('SELECT DISTINCT `order` FROM m_date WHERE `date` = :date AND `apm` = :apm AND `place` = :place');
$orderStmt->bindParam(':date', $selectedDate);
$orderStmt->bindParam(':apm', $selectedApm);
$orderStmt->bindParam(':place', $selectedPlace);
$orderStmt->execute();
$orderList = $orderStmt->fetchAll(PDO::FETCH_COLUMN);
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="./css/styles.css" rel="stylesheet" type="text/css">
<title>日程</title>
</head>
<body>
<div><a style="font-weight: bolder;font-size: 20px;">日程 | Schedule</a></div>
<div class="sl">
<form id="rc-form" action="rc.php" method="POST">
<!-- 第1个下拉框 -->
<select id="date-select" name="date" onchange="handleSelectChange(event);">
<option value="">选择日期</option>
<?php foreach ($dateList as $date): ?>
<option value="<?php echo htmlspecialchars($date); ?>" <?php echo $date === $selectedDate ? 'selected' : ''; ?>><?php echo htmlspecialchars($date); ?></option>
<?php endforeach; ?>
</select>
<!-- 第2个下拉框 -->
<select id="apm-select" name="apm" onchange="handleSelectChange(event);">
<option value="">选择时段</option>
<?php foreach ($apmList as $apm): ?>
<option value="<?php echo htmlspecialchars($apm); ?>" <?php echo $apm === $selectedApm ? 'selected' : ''; ?>><?php echo htmlspecialchars($apm); ?></option>
<?php endforeach; ?>
</select>
<!-- 第3个下拉框 -->
<select id="place-select" name="place" onchange="handleSelectChange(event);">
<option value="">选择场地</option>
<?php foreach ($placeList as $place): ?>
<option value="<?php echo htmlspecialchars($place); ?>" <?php echo $place === $selectedPlace ? 'selected' : ''; ?>><?php echo htmlspecialchars($place); ?></option>
<?php endforeach; ?>
</select>
</form>
</div>
<script>
// 为每个下拉框添加 onchange 事件处理程序
document.getElementById('date-select').addEventListener('change', handleSelectChange);
document.getElementById('apm-select').addEventListener('change', handleSelectChange);
document.getElementById('place-select').addEventListener('change', handleSelectChange);
function handleSelectChange(event) {
// 阻止表单的默认提交行为
event.preventDefault();
// 获取当前选中的值
var date = document.getElementById('date-select').value;
var apm = document.getElementById('apm-select').value;
var place = document.getElementById('place-select').value;
// 使用AJAX发送请求到服务器
var formData = new FormData();
formData.append('date', date);
formData.append('apm', apm);
formData.append('place', place);
fetch('rc.php', {
method: 'POST',
body: formData
})
.then(response => response.text())
.then(data => {
// 假设服务器返回的是表格的HTML内容
document.getElementById('main-content').innerHTML = data;
})
.catch(error => {
console.error("Error fetching data", error);
});
}
</script>
<!--选定之后,显示比赛信息区域 -->
<?php
// 确保输入是安全的
$date = $_POST['date'] ?? '';
$apm = $_POST['apm'] ?? '';
$place = $_POST['place'] ?? '';
// 从记录集中调取出对应信息
$stmt = $pdo->prepare("
SELECT
`order`,
now,
`name`,
time,
schedule,
remark
FROM
m_date
WHERE
`date` = ? AND
`apm` = ? AND
`place` = ?
ORDER BY
`order` ASC
");
$stmt->execute([$date, $apm, $place]);
$order = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
</body>
</html>