Question

I made the comment form and displayed it but i want to display by the same way and the author. I want any user to can type an author name for every new comment. No need to be logged in. How to display the author field? I want to display So here is my php code

<?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);
}

        ?>

And here is the forms with the 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>
Was it helpful?

Solution

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

Then modify your select statement to include the author. Assuming the field is called author in your db

$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)){
    .....
}

and now you have $comment['author'], echo it where you want

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top