Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7800377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:36:57+00:00 2026-06-02T00:36:57+00:00

I creating a simple site with PHP where the users can submit blogs and

  • 0

I creating a simple site with PHP where the users can submit blogs and other users (who are logged in) can post comments on them. I have made a link called “comments” below each blog that when clicked will show / hide all the comments relevant to the specific blog (also if the user is logged in, it will show a form field in which they can submit new comments). So basically each blog will have multiple comments. I have done two different codes for this but they both have the same problem that each comment appears twice (everything else works fine). Could anyone point out why?

mysql_select_db ("ooze"); 
$result = mysql_query ("select * from blog") or die(mysql_error()); 
$i = 1;   
 while($row = mysql_fetch_array($result)) 
 { 
  echo "<h1>$row[title]</h1>"; 
  echo "<p class ='second'>$row[blog_content]</p> ";  
  echo "<p class='meta'>Posted by .... &nbsp;&bull;&nbsp; $row[date] &nbsp;&bull;&nbsp; <a href='#' onclick=\"toggle_visibility('something$i'); return false\">Comments</a><div id='something$i' style='display: none;'>";     
  $i++; 
  $a = $row["ID"];
  $result2 = mysql_query ("select * from blog, blogcomment where $a=blogID") or die(mysql_error()); 
    while($sub = mysql_fetch_array($result2)) 
    { 
     echo "<p class='third' >$sub[commentdate] &nbsp;&bull;&nbsp; $sub[username]</p><p>said:</p> <p>$sub[comment]</p>"; 
     } 
 if ( isset ($_SESSION["gatekeeper"])) 
 { 
    echo '<form method="post" name="result_'.$row["ID"].'" action="postcomment.php"><input name="ID" type = "hidden" value = "'.$row["ID"].'" /><input name="comment" id="comment" type="text" style="margin-left:20px;"/><input type="submit" value="Add comment" /></form>'; 
 }  
 else 
 {  
    echo '<p class="third"><a href="register.html">Signup </a>to post a comment</p>'; 
 } 
echo "</div>"; 
}
mysql_close($conn); 

//second version of inner loop://

if ( isset ($_SESSION["gatekeeper"])) 
{
  while($sub = mysql_fetch_array($result2)) 
  { 
    echo "<p class='third' >$sub[commentdate] &nbsp;&bull;&nbsp; $sub[username] said:</p> <p>$sub[comment]</p>"; 
   }
echo '<form method="post" name="result_'.$row["ID"].'" action="postcomment.php"><input name="ID" type = "hidden" value = "'.$row["ID"].'" /><input name="comment" id="comment" type="text" style="margin-left:20px;"/><input type="submit" value="Add comment" /></form>'; 
}  
else 
{
  while($sub = mysql_fetch_array($result2)) 
  { 
    echo "<p class='third' >$sub[commentdate] &nbsp;&bull;&nbsp; $sub[username] said:</p> <p>$sub[comment]</p>"; 
  }
echo '<p class="third"><a href="register.html">Signup </a>to post a comment</p>'; 
 } 
echo "</div>"; 
} 
mysql_close($conn);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:36:59+00:00Added an answer on June 2, 2026 at 12:36 am

    Your problem lies in this query from the first example.

    $result2 = mysql_query ("select * from blog, blogcomment where $a=blogID")
    

    You have already queried the blog table so there is no need to query it again. Simply changing this to

    $result2 = mysql_query ("select * from blogcomment where $a=blogID")
    

    should solve the problem.

    However there are many things you need to think about.

    • Why are you re-inventing the wheel? There are plenty of good blog applications out there. You’d be better off using one of them.
    • It’s not recommended to use the mysql_ family of functions any more. Go away and learn mysqli_ or better still PDO.
    • You should learn about separation of concerns. At the very least you should make sure your data access/business logic is separate from your display logic. MVC is very common in PHP.
    • You should also learn about JOINs. Even in this simple inline script you have a query within a loop which is not very efficient. You can combine your queries into one (as you’ve tried with the inner query). The difference is the one query should be outside your main loop.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Drupal 6 site with a simple theme I'm creating called NPT.
I have a very simple contact form on my site, Im looking for users
I am creating simple app on Silverlight 4. In folder of View I have
I'm creating a simple mobile website to render specifically on iPhone. I have been
i am creating a related post article feature for my wordpress enabled site.being new
I want to make a POST to a PHP site from a C# WinForm
I'm creating a simple game using HTML and CSS. I have a background image
I'm creating a simple website with php scripts as just kind of a little
I have a non-Drupal site which authenticates with a simple MySQL user database. I
I am creating a web site using php, mysql and zend framework. When I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.