评论时出错

时间:2018-01-25 07:16:52

标签: javascript php html css

我有一个页面可以从数据库中获取帖子,并在页面中显示我已命名为 news-feed.php 的帖子。我的页面看起来像这样:

        

<link rel="stylesheet" type="text/css" href="../Style/news_feed.css"> <!-- Styling -->
<div class="whole-container">
<?php

$connection = mysqli_connect($server_name, $database_username, $database_password, $database_name); //The variables are defined in the **backbone.php** page that we have previously included...
$sql = "SELECT * FROM posts";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)){
    showPosts($row["posted_date"], $row["author"], $row["type"], $row["body"], $row["comments"], $row["id"]);
}





function get($property){ //It gets the $_SESSION["id"] and gets other info about the user using the id.
  require '../includes/backbone.php';
  $connection = mysqli_connect($server_name, $database_username, $database_password, $database_name);
  $sql = "SELECT * FROM users WHERE id = '" . $_SESSION["id"] . "'";
  $result = mysqli_query($connection, $sql);
  while($row = mysqli_fetch_assoc($result)){
    $outcome = $row[$property];
  }
  return $outcome;
}





function showPosts($date, $author, $type, $body, $comments, $id) {
  ?>
  <div class="post">
    <div class="poster"><a href="user.php/<?php echo $author ?>"> <b id="author"><?php echo $author . " posted a new " . $type . ""; ?></a></b> <b id="date"> <?php echo $date ?> </b></div>
    <div class="body"><p><?php echo $body ?></p></div>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
      <?php
      if($type === "query"){
        enableComment();
        if(isset($_POST["postComment"]) && !empty($_POST["comment"])){
          postComment(get("full_name"), date("h:sa"), $_POST["comment"], $id);
      }
      }?>
     <button id="showComments" name="showComments"><i class="fa fa-sort-desc"></i> Show comments</button>
      </form>
    <div class="comments">
      <?php
      if(isset($_POST["showComments"])){
        echo $comments;
      }
      ?>
    </div>
  </div>
  <style>
  .post{
    width: 40%;
    background-color: #ffffff;
    margin-left: 20%;
    margin-top: 2.5%;
    border: 0.2px solid #dddfe2;
    padding: 2px 2px 2px 2px;
    max-height: 80%;
    min-height: 20%;
    overflow: auto;
  }
  .poster{
    display: inline-block;
    width: 100%;
    min-height: 20%;
    max-height: 20%;
    background-color: #ffffff;
    overflow-wrap: break-word;
    text-overflow: ellipsis;
  }
  .poster #author{
    float: left;
    font-family: helvetica;
    font-size: 13px;
  } 
  .poster #date{
    float: right;
    font-family: arial;
    color: grey;
    font-size: 13px;
  }
  .post .body{
    width: 100%;
    background-color: #ffffff;
    float: left;
    overflow-wrap: break-word;
    max-height: 40%;
  }
  .post .body p{
    font-family: helvetica;
  }
  .post #showComments{
    width: 30%;
    background-color: #ffffff;
    border: none;
    cursor: pointer;
    font-family: helvetica;
    color: #3b5998;
  }
  .post #showComments i{
    color: purple;
  }
  .post .comments{
    width: 60%;
    background-color: #ffffff;
    height: 39%;
    max-height: 40%;
    overflow: auto;
    margin-top: 1%;
  }
  .post .comments b{
    font-weight: normal;
    font-family: helvetica;
  }
  #comment{
    width: 50%;
    margin-left: 20%;
    background-color: #ffffff;
    border-bottom: 0.2px solid #dddfe2;
    border-left: none;
    border-right: none;
    border-top: none;
    height: 20px;
    padding-left: 10px;
  }
  #commentButton{
    width: 10%;
    background-color: #ffffff;
    border: 1px solid #dddfe2;
    border-radius: 5px;
    color: #3b5998;
    cursor: pointer;
  }
  #comment:focus{
    outline-width: 0px;
    border-bottom: 1px solid #dddfe2;
  }
    </style>
<?php
} ?>



<?php
function enableComment() {
  ?>
  <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  <input name="comment" type="text" placeholder="Wanna Help?" id="comment">
  <button name="postComment" type="submit" id="commentButton"><i class="fa fa-paper-plane"></i></button>
    </form>
    <?php
}


function postComment($author, $time, $comment, $id){
  require '../includes/backbone.php';
  $connection = mysqli_connect($server_name, $database_username, $database_password, $database_name);
  $sql = "UPDATE posts SET comments = \"'" . $author . "' -> '". $comment . "' @ '".$time . "'\" WHERE id = ". $id. "";
  echo $sql;
}


?>

几乎所有代码都能正常运行,每当我加载页面时,都会显示数据库中的所有内容。但是当我评论帖子时,所有其他帖子都会被评论,其中包含“查询”类型。那么,我该如何解决呢?

我想这是由于显示帖子的while循环,但是如果我放一个break语句,所有其他帖子都没有显示,那我该怎么做呢?

请帮忙......

1 个答案:

答案 0 :(得分:9)

在第一行代码中,您有此评论标记:

<!-- Styling !>

您应该将其更改为<!-- Styling -->,这是在HTML中注释掉代码行的正确语法,请参阅documentation

第二个问题是您的网页已保存为.css,应将其保存为.php