首页 > 解决方案 > 不打印 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>

标签: phpmysql

解决方案


推荐阅读