php - 不打印 sql 数据库中的所有内容
问题描述
我正在尝试为我的学校项目建立一个论坛。现在我想从我的数据库中取出数据,这样我就可以打印它,人们可以看到它并做出反应。现在我遇到的问题是它只打印评论者的帖子而不是创作者的帖子。我将添加一些图片和代码,以便你们理解。(PS。我在上一页有一个表格,你可以搜索) 数据库 网站
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./style.css">
<title><?php echo $_GET["search"];?></title>
</head>
<body>
<?php
//navbar
include "./include/nav.php";
//database connection
include "./include/conn.php";
$search = $_GET["search"];
if($search == ""){
// if empty return to index
echo "
<script>alert('thread does not exist')</script>
<script>window.location = './index.php'</script>
";
} else {
//check if data exist
$sql = "SELECT thread, post, username, status FROM post WHERE thread = ? ";
$query = $conn->prepare($sql);
$query->execute(array($search));
$data = $query->fetch();
if($data){
// print data if exist
echo "<div class='padding'>";
foreach ($query as $test) {
echo "<tr>";
echo "<td>" . $test["thread"] . "</td>";
echo "<td>" . $test["post"] . "</td>";
echo "<td>" . $test["username"] . "</td>";
echo "<td>" . $test["status"] . "</td>";
echo "</tr>" . "</br>";
}
echo "<button onclick='comment()'>Comment</button>";
echo "</div>";
echo "<div id='test'>";
} else {
// if data does not exist return to index
echo "
<script>alert('thread does not exist')</script>
<script>window.location = './index.php'</script>
";
}
}
?>
<script>
// if comment btn is pressed save thread name and send to comment.php
function comment() {
document.getElementById("test").innerHTML =
"<?php
session_start();
$_SESSION['thread'] = $data['thread'];
?>";
window.location.assign("./comment.php")
}
</script>
</body>
</html>
解决方案
推荐阅读
- entity-framework-core - 保存更改时缓存下降
- javascript - 关闭 Eclipse 2020-06 Javascript 验证说明
- python - 从 HTTP 请求中获取附加的 PDF 文件
- java - Firebase 聊天应用接收通知
- python - 如何从 C 函数签名中确定 Python3 ctypes .argtypes?
- java - 如何从 Maven 输出中删除 [INFO] 和 [ERROR] 标签?
- reactjs - 使用材质 ui TextField 显示 imask 的问题
- python - 如何在 AWS EC2 上运行 python 代码然后使用 POST?
- vhdl - 在 VHDL 中初始化矩阵需要大量类型的逻辑单元块
- elasticsearch - Elasticsearch 搜索时间分析器非常慢