简体   繁体   English

通过页面ID获取评论

[英]Get comments by the ID of the page

Hello i have created a databse which store the information sended by the form on my page. 您好,我创建了一个数据库,用于存储表单在我的页面上发送的信息。 The structure of the database is this : 数据库的结构是这样的:

cid(comment id)    uid(value='Anonymous')   id(of the page)   date     message(text of the message)

So when i goes to a particular page of my website, for example http://miostio.com/page.php?id=15 here i can put a comment by a form which send the information that u can see up in my database. 因此,当我转到网站的特定页面(例如http://miostio.com/page.php?id=15)​​时 ,我可以通过表格发送评论,该表格发送您可以在数据库中看到的信息。

Now on my database are stored the id of the page in which i have putted the comments, but when i try to see the comment in that page by the function : getComments($conn); 现在,在数据库中存储了我在其中放置评论的页面的ID,但是当我尝试通过以下功能在该页面中查看评论时:getComments($ conn); ,here are displayed all the comments saved in the database and not only the comments with the id of the page. ,此处将显示数据库中保存的所有评论,而不仅仅是显示具有页面ID的评论。

I want that the comments displayed corresponds to the id of the page, in page with id(15) display the comment of the page with id(15), in page with id(10) display the comment of the page with id(10) exc ... 我希望显示的注释与页面的ID相对应,在ID(15)的页面中显示ID(15)的页面的注释,在ID(10)的页面中显示ID(10)的页面的注释)ex ...

PHP --> form that send data PHP->发送数据的表单

echo "<form method='POST' action='".setComments($conn)."'>
                <input type='hidden' name='id' value='".$row['id']."'>
                <input type='hidden' name='uid' value='Anonymous'>
                <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
                <textarea name='message'></textarea><br>
                <button name='commentSubmit' type='submit' class='comm-btn'>Comment</button>
                </form>";

                getComments($conn);

other PHP CODE which contain the function called by the form 其他PHP CODE,其中包含由表单调用的函数

                        function setComments($conn) {
                    if (isset($_POST['commentSubmit'])){
                        $uid = $_POST['uid'];
                        $date = $_POST['date'];
                        $message = $_POST['message'];
                        $id = $_POST['id'];

                        $sql = "INSERT INTO comments (uid, date, message, id) VALUES ('$uid', '$date', '$message', '$id')";
                        $result = $conn->query($sql);
                    }
                }

                    function getComments ($conn) {
                        $sql = "SELECT * FROM comments WHERE id = id ORDER BY cid DESC";
                        $result = $conn->query($sql);
                        while($row = $result->fetch_assoc()) {
                            echo "<div class='comment-box'><p>";
                                echo $row['uid']."<br>";
                                echo $row['date']."<br>";
                                echo nl2br($row['message']);
                            echo "</p></div>";
                        }
                    }
"SELECT * FROM comments WHERE id = id ORDER BY cid DESC"

id always === id id始终=== id

You need to give a real id there... 您需要在那里提供真实的身份证件...

"SELECT * FROM comments WHERE id = $id ORDER BY cid DESC"

Would consider protecting it from sql injection like this: 将考虑像这样通过sql注入保护它:

"SELECT * FROM comments WHERE id = " . (int)$id . "ORDER BY cid DESC"

You got at least two mistakes (I don't know yet if the rest is ok and working): 您至少遇到了两个错误(我不知道其余的是否可以正常工作):

1st: Your sql statement doesn't include the variable you're aming for, it just says kinda 'if 1=1'. 1st:您的sql语句不包含您要查找的变量,只是说“如果1 = 1”。 So change it to: 因此将其更改为:

$sql = "SELECT * FROM comments WHERE id = $id ORDER BY cid DESC"

2nd: You don't have yet $id available in that function. 第二:该功能还没有$ id可用。 So include that: 因此包括:

function getComments ($conn) {
   $id = intval($_POST['id']); // cast to int for security
   $sql = "SELECT * FROM comments WHERE id = $id ORDER BY cid DESC";
   $result = $conn->query($sql);
   while($row = $result->fetch_assoc()) {
       echo "<div class='comment-box'><p>";
       echo $row['uid']."<br>";
       echo $row['date']."<br>";
       echo nl2br($row['message']);
       echo "</p></div>";
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM