简体   繁体   English

使用PHP从数据库检索到的数据未以HTML格式显示

[英]Data not displaying in HTML format retrieved from database using PHP

I am having an issue where I am having a user enter information into a textarea and and having that information stored in a mysql database. 我遇到一个问题,我要让用户在文本区域中输入信息,并将该信息存储在mysql数据库中。 The user is allowed to insert HTML into the textarea such as: 允许用户将HTML插入文本区域,例如:

<ul>
  <li>Test</li>
</ul>

However, I am having trouble seeing why the data that is being retrieved from the database to display the data the user has entered, is not showing the correct HTML format the user requested. 但是,我很难理解为什么从数据库检索来显示用户输入数据的数据没有显示用户请求的正确HTML格式。

This is what I have: Displaying the information: 这就是我所拥有的:显示信息:

<?php
function display ($result) {
  if (mysql_num_rows($result) > 0) {
    echo "<form action='scripts/submit.php' method='post'>";
      echo "<input type='submit' name='post' value='Post'/>";
      echo " | ";
      echo "<label for='searchDate'>Search Archives By Date:</label>";
      echo "<input type='text' name='searchDate'>";
      echo "<input type='submit' name='submitDate' value='Search'/>";
      echo " | ";
      echo "<label for='searchTicket'>Search Archives By Ticket:</label>";
      echo "<input type='text' name='searchTicket'/>";
      echo "<input type='submit' name='submitTicket' value='Search'/>";
      echo "<hr/>";
    echo "</form>";
    echo "<table border='1' id='resultTable'>";
      while($row = mysql_fetch_assoc($result)) {
      $replace = str_replace ("\n","<br/>",$row['content']);
      echo "<tr>";
        echo "<td><span id='boldText'>Entry By:</span> ".$row['user']." | <span id='boldText'>Description:</span> ".$row['description']." | <span id='boldText'>Entry Date:</span> ".$row['date']."</td>";
      echo "</tr>";
      echo "<tr>";
        echo "<td>";
            echo "<p>".$replace."</p>";
        echo "</td>";
      echo "</tr>";
      echo "<tr>";
        echo "<td>";
          echo "<form action='scripts/submit.php' method='post'>";
            echo "<input type='hidden' name='count' value='$row[count]'/>";
            echo "<input type='submit' name='edit' value='Edit'/>";
            echo "<input type='submit' name='delete' value='Delete'/>";
          echo "</form>";
        echo "</td>";
      echo "</tr>";
          }
          echo "</table>";
      }
      else {
        echo "<h3 id='noResults'>Nothing has been posted yet.  Click 'Post' to add something to the whiteboard.</h3>";
        echo "<form action='scripts/submit.php' method='post'>";
          echo "<input type='submit' name='post' value='Post'/>";
        echo "</form>";
      }
}

?>

Add Post Logic: 添加发布逻辑:

if(isset($_POST['add_post'])) {
$content = mysql_real_escape_string($_POST['content']);
$description = mysql_real_escape_string($_POST['desc']);

  $query = "INSERT INTO $table_name (user, description, content)
            VALUES ('$_SESSION[logged_in]','$description', '$content')";
  $result = mysql_query($query);
  session_write_close();
  header('Location: ../whiteboard.php');
}

For some reason the example above will not work, but this will: 由于某些原因,上面的示例将无法正常工作,但这将:

<p style="font-weight: 900;">Test</p>

尝试解析,然后使用htmlentities()nl2br()插入数据库,并在取回时进行相反的操作。

If you're viewing the database entries in a browser, you're going to see the result of the formatted HTML, not the actual markup - use htmlentities($result) if that's what you are after. 如果在浏览器中查看数据库条目,则将看到格式化HTML的结果,而不是实际的标记-如果要这样做,请使用htmlentities($ result)。

View the page source to see what is being retrieved and see how it differs from what you expect to see. 查看页面源以查看正在检索的内容,并查看与您期望的内容有何不同。

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

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