如何将SQL查询的电影数据表嵌入现有PHP页面的HTML表格中
实现步骤
1. 修复查询逻辑的错误
你现有的查询代码存在两处遗漏,先修正:
- 写完SQL语句后没有执行查询获取结果集
- 没有填写实际的数据库名参数
修正后的查询逻辑可以直接嵌入到页面中,不需要单独存为外部文件,避免重复创建数据库连接:
<?php // 如果你已经在index.php里写了数据库连接,可以删掉下面这段连接代码直接复用 $servername = "localhost"; $username = "Admin"; $password = "root"; $dbname = "替换为你实际的数据库名"; // 这里要填你存movies表的数据库名 // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 接收筛选参数(支持按分类筛选电影) $selected_genre = isset($_POST['genre']) ? $_POST['genre'] : ''; // 构建查询SQL if (!empty($selected_genre) && $selected_genre != 'Select') { $sql = "SELECT mv, genre FROM movies WHERE genre = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("s", $selected_genre); $stmt->execute(); $result = $stmt->get_result(); } else { $sql = "SELECT mv, genre FROM movies"; $result = $conn->query($sql); } // 渲染表格 if ($result->num_rows > 0) { echo '<table width="100%" border="1">'; echo '<tr><th>电影名称</th><th>分类</th></tr>'; while ($row = $result->fetch_assoc()) { echo '<tr><td>' . htmlspecialchars($row["mv"]) . '</td><td>' . htmlspecialchars($row["genre"]) . '</td></tr>'; } echo '</table>'; } else { echo '<p>暂无符合条件的电影数据</p>'; } // 关闭连接 $conn->close(); ?>
2. 整合到现有Movies页面
你现有页面还缺少表单标签包裹下拉框和提交按钮,否则点击提交不会有效果,完整修改后的页面代码如下:
<?php session_start(); if (!isset($_SESSION['loggedin'])) { header('Location: index.html'); exit; } ?> <html> <head> <meta charset="utf-8"> <title>电影列表</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <nav class="navtop"> <div> <h1>BB.com</h1> <a href="profile.php">个人资料</a> <a href="logout.php">退出登录</a> <a href="home.php">首页</a> </div> </nav> <h1><u> Movies </u> </h1> <div class="container"> <div class="wrapper"> <h1>分类筛选</h1> <div class="data"> <!-- 新增form表单,提交方式为post --> <form method="post" action=""> <select name="genre"> <option>Select</option> <option>Horror</option> <option>Romance</option> <option>Thriller</option> <option>Detective</option> <option>Comedy</option> <option>Drama</option> <option>Spy</option> <option>Fantasy</option> <option>Sc-Fi</option> </select> <input type="submit" name="b1" class="submit" value="筛选"/> </form> </div> <!-- 这里插入查询渲染代码 --> <?php // 如果你已经在index.php里写了数据库连接,可以删掉下面这段连接代码直接复用 $servername = "localhost"; $username = "Admin"; $password = "root"; $dbname = "替换为你实际的数据库名"; // 这里要填你存movies表的数据库名 // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 接收筛选参数 $selected_genre = isset($_POST['genre']) ? $_POST['genre'] : ''; // 构建查询SQL if (!empty($selected_genre) && $selected_genre != 'Select') { $sql = "SELECT mv, genre FROM movies WHERE genre = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("s", $selected_genre); $stmt->execute(); $result = $stmt->get_result(); } else { $sql = "SELECT mv, genre FROM movies"; $result = $conn->query($sql); } // 渲染表格 if ($result->num_rows > 0) { echo '<table width="100%" border="1">'; echo '<tr><th>电影名称</th><th>分类</th></tr>'; while ($row = $result->fetch_assoc()) { echo '<tr><td>' . htmlspecialchars($row["mv"]) . '</td><td>' . htmlspecialchars($row["genre"]) . '</td></tr>'; } echo '</table>'; } else { echo '<p>暂无符合条件的电影数据</p>'; } // 关闭连接 $conn->close(); ?> </div> </div> </body> </html>
注意事项
- 请把代码中的
替换为你实际的数据库名改成你自己的数据库名称 - 如果你的index.php已经写了数据库连接逻辑,可以把页面中重复的连接代码删掉,直接复用连接即可
- 这里用了预处理语句查询,避免SQL注入风险,比直接拼接SQL更安全
内容的提问来源于stack exchange,提问作者ProofDoubloon
相关产品推荐
相关产品推荐

