You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将数据库不同行数据关联到按钮并在预订页面展示对应活动信息?

解决活动按钮关联对应信息的问题

我来帮你搞定这个问题!你现在的核心问题是没有正确传递和接收活动ID,导致预订页面把所有活动信息都列出来了,而不是对应点击的那一个。咱们一步步修改:

第一步:修复列表页的活动ID传递

你当前的列表页代码里,隐藏字段的value值写的是$eventId,但这个变量既没定义,也没有从数据库行里获取对应活动的ID,而且因为是在单引号包裹的字符串里,PHP不会解析这个变量。修改如下:

<?php 
$sql = "SELECT * from events;"; 
$result = mysqli_query($conn, $sql); 
$resultCheck = mysqli_num_rows($result); 

if($resultCheck > 0) { 
    while($row = mysqli_fetch_assoc($result)) { 
        // 假设你的活动ID字段名为EVENT_ID,根据实际数据库字段调整
        $eventId = $row['EVENT_ID'];
        echo ' 
        <div class="full border" id="border"> 
            <p> 
                <img src="event.jpg" alt="活动图片" id="event1"/> 
                <h1>'.$row['EVENT_NAME'].'</h1> 
                <a>'.$row['EVENT_DESC'].'</a> 
                <p align="right"> 
                    <form action="booking.php" method="get"> 
                        <!-- 这里改成从数据库行获取真实的活动ID -->
                        <input type="hidden" name="eventId" value="'.$eventId.'"> 
                        <button type="submit" class="button">More information</button> 
                    </form> 
                </p> 
            </p> 
        </div> 
        <br/><br/> 
        '; 
    } 
} 
?>

第二步:修复预订页的活动信息查询

现在预订页需要从URL里获取eventId参数,然后只查询对应ID的活动信息,而不是所有活动。同时为了避免SQL注入风险,建议使用预处理语句:

<?php 
// 先检查是否接收到eventId参数
if(isset($_GET['eventId']) && !empty($_GET['eventId'])) {
    $eventId = $_GET['eventId'];
    
    // 使用预处理语句查询,防止SQL注入
    $sql = "SELECT * from events WHERE EVENT_ID = ?";
    $stmt = mysqli_prepare($conn, $sql);
    mysqli_stmt_bind_param($stmt, "i", $eventId); // "i"表示整数类型,若ID是字符串则用"s"
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    
    $resultCheck = mysqli_num_rows($result); 
    if($resultCheck > 0) { 
        $row = mysqli_fetch_assoc($result); // 因为只查单个活动,不需要while循环
        echo ' 
        <h1>'.$row['EVENT_NAME'].'</h1> 
        <table> <!-- 补上之前遗漏的<table>标签 -->
            <tr> 
                <td><h5>Venue</h5></td> 
                <td><h5>Date</h5></td> 
                <td><h5>Time</h5></td> 
                <td><h5>Joining Price</h5></td> 
                <td><h5>Seats left</h5></td> 
            </tr> 
            <tr> 
                <td><h5>'.$row['EVENT_VENUE'].'</h5></td> 
                <td><h5>'.$row['START_DATE'].' - '.$row['END_DATE'].'</h5></td> 
                <td><h5>'.$row['START_TIME'].'-'.$row['END_TIME'].'</h5></td> 
                <td><h5>'.$row['TICKET_PRICE'].'</h5></td> 
                <td><h5>'.$row['SEATS_AVAILABILITY'].'</h5></td> 
            </tr> 
        </table> 
        <form id="form" style="font-size: 15px;"> 
            <fieldset class="form"> 
                <legend>Please fill this form:</legend> 
                <label for="name">Enter your name (same as IC) : </label> 
                <input type="text" class="a" id="name"></br><br> 
                <label for="studentId">Enter your student ID : </label> 
                <input type="text" class="a" id="studentId"></br><br> 
                <label for="email">Enter your email: </label> 
                <input type="email" class="a" id="email"></br><br> <!-- 改成email类型更符合语义 -->
                <label for="phoneNumber">Enter your phone number: </label> 
                <input type="tel" class="a" id="phoneNumber"></br><br> <!-- 改成tel类型更符合语义 -->
                Select your preferred payment method:</br> 
                <input type="radio" id="type1" name="payment" value="eWallet"> 
                <label for="type1">E-wallet</label></br> 
                <input type="radio" id="type2" name="payment" value="onlineBanking"> 
                <label for="type2">Online banking</label></br> 
                <input type="radio" id="type3" name="payment" value="creditCard"> 
                <label for="type3">Credit card</label></br><br> 
                <button type="button" onclick="window.location.href=\'records.php\'">Submit</button> <!-- 转义单引号避免语法错误 -->
            </fieldset> 
        </form> 
        <div style="padding: 10px;margin-left: 20%; margin-right: 20%; border: 1px dotted black;"> 
            <h3 align="center">Terms and conditions: </h3> 
            <p align="left"> '.$row['TERMS'].' </p> 
        </div> 
        '; 
    } else {
        echo "<p>找不到对应的活动信息!</p>";
    }
} else {
    echo "<p>请选择一个活动查看详情!</p>";
}
?>

额外注意点

  • 确保你的数据库里活动表的ID字段名和代码里的EVENT_ID一致,如果实际字段名是其他(比如id),记得替换成对应的名称。
  • 预订页里的表单提交按钮目前只是跳转页面,没有传递预订数据和活动ID,后续如果要保存预订记录,需要修改表单的提交逻辑,把表单数据和活动ID一起提交到处理页面。

内容的提问来源于stack exchange,提问作者user14053908

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 07:43:14