0

我制作了评论表并显示了它,但我想以同样的方式和作者显示。我希望任何用户都可以为每条新评论键入作者姓名。无需登录。如何显示作者字段?我想显示所以这是我的 php 代码

<?php
if(isset($_POST['addComment']))
    {
            $post_id = $_POST['post'];
            $comment = $_POST['comment'];
            $author = $_POST['author'];
        mysql_query("INSERT INTO comments (post_id, comment, author) VALUES('$post_id','$comment', '$author')") or die (mysql_error);
}

        ?>

这是带有 php 的表格:

<div id="comments">
<?php 
        $post_id = $_GET['post'];
        $q = mysql_query("SELECT comment FROM comments WHERE post_id ='$post_id' ORDER BY comment_id DESC");
        while($comment = mysql_fetch_assoc($q)){
            ?>
<ul class="commentlist">
          <li class="comment_odd">
            <div class="author"><img class="avatar" src="images/demo/avatar.gif" width="32" height="32" alt="" /><span class="name"><a href="#"><?php echo $comment['author']; ?></a></span> <span class="wrote">wrote:</span></div>
            <p><?php echo $comment['comment']; ?><br /><br /></p>
          </li>


        <?php
        }
        ?>

        Коментар:<br />
        <form action="" method="author">
<p>
            <input type="text" name="name" id="name" value="" size="22" />
            <label for="name"><small>Name (required)</small></label>
            <input type="hidden" name="author" value="<?php echo $_GET['author']; ?>" />
          </p>
        </form>
<form action="" method="post">

          <p>
            <textarea name="comment" id="commentField" cols="100%" rows="10"></textarea><br />
            <label for="comment" style="display:none;"><small>Comment (required)</small></label>
            <input type="hidden" name="post" value="<?php echo $_GET['post']; ?>" />
    <input type="submit" name="addComment" value="Добави" />
          </p>
          </form>
4

1 回答 1

0
if(isset($_POST['addComment']) && $_POST['addComment'] != '')
{
    $post_id = $_GET['post'];
    $q = mysql_query("YOUR INSERT STATMENTHERE");
}

然后修改您的选择语句以包括作者。假设该字段在您的数据库中称为作者

$post_id = $_GET['post'];
$q = mysql_query("SELECT comment, author FROM comments WHERE post_id ='$post_id' ORDER BY comment_id DESC");
while($comment = mysql_fetch_assoc($q)){
    .....
}

现在你有了 $comment['author'],在你想要的地方回显它

于 2014-01-28T20:13:18.480 回答