0

我正在为我的网站制作一个 PHP 评论框。这是代码:

$con = mysql_connect($hostname,$username,$password); // Connect to MySQL database
if (!$con)
  {
  die("Could not connect: " . mysql_error()); 
  }
mysql_select_db($dbname);
if(isset($_POST["submit"]))
{
    $comment=$_POST["comment"];
    $q="INSERT INTO comments_table (comments) VALUES ('$comment')"; // Could also be (\"comment\")
    mysql_query($q);
} 
$q="SELECT comments FROM comments_table";
$result=mysql_query($q);
while($row=mysql_fetch_array($result))
{
    // List the comments - how could I get some markup between each to make each have it's own area?
    echo $row['comments']."";
}
?>
<html>
<body>
<form method="post" action="/comments-test.php">
<textarea name="comment" rows=30 cols=10></textarea>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

我想知道的是我应该如何让每条评论都有自己的框或标记。每个评论之间不能是相同的HTML(例如,,</div><div class="comment">),否则评论的末尾会有一些评论框没有结束标签,而开头有一个杂散的结束标签。我是 SQL 新手,但不是 PHP 新手。我该怎么做?

4

3 回答 3

1
  // List the comments - how could I get some markup between each to make each have it's own area?
    echo '<div class="comment">'.$row['comments']."</div>";

那行得通吗?

于 2012-12-26T21:41:50.047 回答
0

我不完全明白你在说什么,但你的意思是这样的:

while($row=mysql_fetch_array($result))
{
    // List the comments - how could I get some markup between each to make each have it's own area?
    echo "<div class='comment'>".$row['comments']."</div>";
}

如果不是,那么我不确定你在说什么。

于 2012-12-26T21:41:58.393 回答
0

只需将您的评论包装到适当div的 s

while($row=mysql_fetch_array($result))
{
    // List the comments - how could I get some markup between each to make each have it's own area?
    echo '<div class="comment">', $row['comments'], '</div>';
}
于 2012-12-26T21:43:53.477 回答