Hello.
I am using a book called Blog Design Solutions by Andy Budd et al to build my own blog (chapter 7). The site is running on localhost and pretty much everything works ... except the 'add a comment' system. A form in the sidebar of the 'posts' page invites users to add comments. When the form is filled in and the 'post comment' button is hit, the machine whirrs through its paces briefly then presents a page with sidebars correctly formatted but with a bit of a mess in place of the expected content:

1 - the post has disappeared
2 - three error messages , each reading:

Notice: Undefined index: myposts (one reads mycomments) in /Library/WebServer/Documents/test/post.php on line (number)

The code for and following each of the salient line numbers reads:

<?php
if ($myposts) {
		$sql = "SELECT comment_id, name, website, comment FROM comments WHERE post_id = $post_id";
		$result3 = mysql_query($sql);
		$mycomments = mysql_fetch_array($result3);
	} 
?>

:

<?php
if($myposts) {
	do {
		$post_id =$myposts["post_id"];
		$title = $myposts["title"];
		$post = format($myposts["post"]);
		$dateattime = $myposts["dateattime"];
		echo "<h3>$title</h3>\n";
		//echo "<h4>posted on $dateattime</h4>\n";
		echo "<div class='post'>\n $post \n</div>";
		echo "<div class='right'>posted on $dateattime</div>";
	}   while ($myposts = mysql_fetch_array($result));
} else {
	echo"<p>there is no post matching a post_id of $post_id.</p>";
}
?>

:

<?php
if($mycomments) {
	echo "<dl>";
	do {
		$comment_id = $mycomments["comment_id"];
		$name = $mycomments["name"];
		$website = $mycomments["website"];
		$comment = format($mycomments["comment"]);
		if ($website !="") {
			echo "<dt><a href='$website'>$name</a> wrote:</dt>\n";
		} else {
			echo "<dt>$name wrote:</dt>\n";
		}
		echo "<p><dd>$comment</dd></p>\n";
	  } while ($mycomments = mysql_fetch_array($result3));
	  echo "</dl>";
	 } 	else {
	 	echo"<p class='central'>use the add a comment form to leave a comment.</p>";
	 }
?>

All the php tags are present. I see from checking in phpMyAdmin that the comments reach the database.
I'm a bit flumoxed on this one and knowing what 'undefined index' means might help me to define it.
I run a Mac iBook G4 on OSX 10.4.10 using php 5.2.0, mySql 5.0.24a.
Any help would be greatly appreciated.
Many thanks
David

    First, please wrap your code samples here in

     tags[/url].
    
    Second, please indicate to us which line is being referred to by the error message.
    
    The undefined index notice indicates that you are referring to an array element that does not exist, e.g.:
    [code]
    <?php
    $test = array('one' => 1, 'two' => 2);
    $foobar = $test['[color=red]three[/color]'];
    

      The index of an array refers to an array element.

      //$varName[index_name] = value
      $a['idx1'] = 1;
      $a['idx2'] = 2;
      
      echo $a['idx2'];
      //prints 2.
      
      echo $a['idx3'];
      //erros out because 'idx3' does not exist.
      
      

      The array value you are trying to access does not exist. Check the functions that build this array. If the data is coming from a form, keep in mind that those are $POST/$GET, depending on the form method, by default.

        Thanks NogDog for clarifying the

         tags.
        Thanks too to Protato for your clear description of what's missing. It's 1am here in the uk and I need to sleep now. I'll check in out later and let you know how it turned out.
        Best wishes.
        David

          Hello again
          I'm still having problems with comments.
          The page 'posts' (see posts.gif - attached) currently displays posts as intended. What I'm trying to do is enable comments by having users complete the add comments form and hit the post comment button. The comment is then supposed to appear in the comments box beneath the text of the post.
          What currently happens when the post comment button is hit is the disappearence of the post and a series of 3 undefined variable error meassages:
          Undefined variable: myposts ... line 54
          Undefined variable: myposts ... line 84
          Undefined variable: mycomments ... 107
          (I have altered the actual line numbers to reflect those in the abbreviated code below)
          I had thought my variables had been defined at lines 14, 57 and 84. In fact the $myposts snippet does work when the post is called from the index page, the archive page and the search results page.
          Interestingly, the error messages have changed from Undefined index (last night) to Undefined variable (tonight). I'm not sure of what I'm missing.
          The relevant code is:

           	<?php
          	//open connection to database
          	include("../db_connect.php");
          
          //get post_id from query string
          $post_id = (isset($_REQUEST["post_id"]))?$_REQUEST["post_id"]:"";
          
          
          //if post_id is a number get post from database
          if (preg_match("/^[0-9]+$/", $post_id)) {
          	$sql = "SELECT post_id, title, post, DATE_FORMAT(postdate, '%e %b %Y at  %H:%i') 
                  AS dateattime FROM posts WHERE post_id=$post_id LIMIT 1";
          	$result = mysql_query($sql);
          14		$myposts = mysql_fetch_array($result); //I thought this defined variable mypost
          	}
          
          //format $text
          include("functions.php");
          
          //if comment has been submitted and post exists then add comment to database
          if (isset($_POST["postcomment"]) != "") {
          	$posttitle = addslashes(trim(strip_tags($_POST["posttitle"])));
          	$name = addslashes(trim(strip_tags($_POST["name"])));
          	$email = addslashes(trim(strip_tags($_POST["email"])));
          	$website = addslashes(trim(strip_tags($_POST["website"])));
          	$comment = addslashes(trim(strip_tags($_POST["comment"])));
          
          	$sql = "INSERT INTO comments
          		(post_id,name,email,website,comment)
          		VALUES ('$post_id', '$name', '$email', '$website', '$comment')";
          	$result2 = mysql_query($sql);
          	if (!$result2) {
          		$message = "failed to insert comment.";
          	} else {
          		$message = "comment added.";
          		$comment_id = mysql_insert_id();
          
          		//send yourself an email when a comment is successfully added
          		$emailsubject = "comment added to: ".$posttitle;
          
          		$emailbody ="comment on '".$posttitle."'"."\r\n"
          	 	."http://www.thewrightline.co.uk/post.php?post_id=
                           ".$post_id ."#c".$comment_id."\r\n\r\n"
          	 	.$comment."\r\n\r\n"
          	 	.$name." (".$website.")\r\n\r\n";
          		$emailbody = stripslashes($emailbody);
          
          		$emailheader = "from: ".$name." <".$email.">\r\n"."reply-to: ".$email;
          
          		mail("david@thewrightline.com", $emailsubject, $emailbody, $emailheader);
          		//direct to post page to eliminate repeat posts
          		header("Location: post.php?post_id=$post_id&message=$message");
          	}
          }
          	//pull comments from database
          54		if ($myposts) { //this line returns error message 'undefined variable: myposts
          			$sql = "SELECT comment_id, name, website, comment FROM comments 
                               WHERE post_id=$post_id";
          			$result3 = mysql_query($sql);
          57	$mycomments = mysql_fetch_array($result3); //I thought this defined 
                                                          //variable mycomments
          		}
          	?>
          
          <html>
          
          <head>
          <title>posts</title>
          </head>
          
          <form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>">
          <input type="hidden" name="post_id" value="<?=$post_id ?>" />
          <input type="hidden" name="posttitle" value="<?=$title ?>" />
          <h4>add a comment:</h4>
          <?php
          if (isset($message)) {
          	echo "<p>".$_POST["message"]."</p>";
          	}
          	?>
          	<p>name: <br /><input name="name" type="text" /></p>
          	<p>email: <br /><input name="email" type="text" /></p>
          	<p>website: <br /><input name="website" type="text" /></p>
          	<p>comment: <br /><textarea name="comment" cols="20" rows="10"></textarea></p>
                <p class="align"><input type="submit" name="postcomment" value="post comment" />
             </p>
          </form>
          
          <?php
          84	 if ($myposts) {//displays post - but not after add a comment button is hit, 
          			         //after which it  returns error undefined index myposts
          		do {
          			$post_id = $myposts["post_id"];
          			$title = $myposts["title"];
          			$post = format($myposts["post"]);
          			$dateattime = $myposts["dateattime"];
          			echo "<h3>$title</h3>\n";
          			echo "<div class='post'>\n $post \n</div>";
          			echo "<div class='right'>posted on $dateattime</div>";
          		}   while ($myposts = mysql_fetch_array($result));
          	} else {
          		echo"<p>there is no post matching a post_id of $post_id.</p>";
          	}
          	?>
          
          <h4>comments</h4>
          <?php
          107 if ($mycomments) { //returns error message undefined index mycomments
          		echo "<dl>";
          		do {
          			$comment_id = $mycomments["comment_id"];
          			$name = $mycomments["name"];
          			$website = $mycomments["website"];
          			$comment = format($mycomments["comment"]);
          			if ($website !="") {
          				echo "<dt><a href='$website'>$name</a> wrote:</dt>\n";
          			} else {
          				echo "<dt>$name wrote:</dt>\n";
          			}
          			echo "<p><dd>$comment</dd></p>\n";
          		  } while ($mycomments = mysql_fetch_array($result3));
          		  echo "</dl>";
          		 } 	else {
          		 	echo"<p class='central'>use the <em>add a comment</em> form to leave a 
          comment.</p>";
          		 }
          		 ?>
          		</body>
          		</html>  

          I'd be very grateful for any further help.
          Many thanks
          David

            My guess is that the following if condition is failing, thus $myposts is not being set:

                //if post_id is a number get post from database
                if (preg_match("/^[0-9]+$/", $post_id)) {
                    $sql = "SELECT post_id, title, post, DATE_FORMAT(postdate, '%e %b %Y at  %H:%i')
                        AS dateattime FROM posts WHERE post_id=$post_id LIMIT 1";
                    $result = mysql_query($sql);
            14        $myposts = mysql_fetch_array($result); //I thought this defined variable mypost
                }
            

            You might want to add an else right after that that outputs an error message. Something like:

                //if post_id is a number get post from database
                if (preg_match("/^[0-9]+$/", $post_id)) {
                    $sql = "SELECT post_id, title, post, DATE_FORMAT(postdate, '%e %b %Y at  %H:%i')
                        AS dateattime FROM posts WHERE post_id=$post_id LIMIT 1";
                    $result = mysql_query($sql);
            14        $myposts = mysql_fetch_array($result); //I thought this defined variable mypost
                }
                else
                {
                    user_error("Invalid value for post_id: '$post_id'", E_USER_ERROR);
                }
            

            You'll eventually want a more graceful way to deal with an invalid value there, but for now this will let you know if my suspicion is correct.

              Interesting idea - I haven't come across user_error previously, so I looked it up on php.net - it triggers an error message, right?

               //if post_id is a number get post from database universal
                 if (preg_match("/^[0-9]+$/", $post_id)) {
              	   $sql = "SELECT post_id, title, post, DATE_FORMAT
                     (postdate, '%e %b %Y at %H:  %     i') AS dateattime FROM posts
              	   WHERE post_id=$post_id LIMIT 1";
              	   $result = mysql_query($sql);
              	   $myposts = mysql_fetch_array($result);
                 } else {
                   user_error("Invalid value for post_id: '$post_id'", E_USER_ERROR); 
              	       	}
              
              19 include("functions.php");

              returns the following error message:
              Parse error: syntax error, unexpected T_INCLUDE in /Library/WebServer/Documents/test/post.php on line 19.
              If I comment out line 19, the syntax error jumps to the next line. I guess that would go on to the end of the script if only I had the time!
              That's helpful. Any ideas what might be wrong in lines 9 to 14? I guess it's the same code that works until adding a comment is attempted.
              Much appreciate your help, NogDog.
              David

                Just some pretty simple debugging to find out what's going wrong. As Nogg mentioned the else with an error is good, do more. Looks like you're not hitting the else though? Just add a few more echo's in so you can see what your script is doing.

                
                   echo "going to check for post_id<br>";
                
                   if (preg_match("/^[0-9]+$/", $post_id)) {
                       echo "post_id is set with ".$post_id;
                       $sql = "SELECT post_id, title, post, DATE_FORMAT
                       (postdate, '%e %b %Y at %H:  %     i') AS dateattime FROM posts
                       WHERE post_id=$post_id LIMIT 1";
                
                  //run this to make sure your query is not empty, assuming you are making it this far.
                  echo $sql."<br>";
                
                   //add the die for debug so you know if it's an error in the query.
                   $result = mysql_query($sql) or die("failed to get post for id $post_id<br>".mysql_error;
                
                   $myposts = mysql_fetch_array($result);
                   //overkill, as all the other messages will get you going, but another useful debug tool, this will print out the array keys and values for myposts, the pre tags make it a more readable formatting.
                  echo "<pre>";
                  print_r($myposts);
                 echo "</pre>";
                
                   } else {
                       //one of my servers doesn't display any php errors, so I use echo out of habbit.
                       echo "post_id check failed, I got passed $post_id for the post_id";
                   } 
                

                If that doesn't make it clearer why its not assigning to the myposts variable, it should at least give you enough information to get more out of us.

                As for the error that keeps skipping down a line, check your braces ('{' & '}') to make sure you are closing each one that gets opened.

                  Thanks Protato. I'll try it out later and let you know the outcome.
                  All the best
                  David

                    Thanks for that, Protato. I've not done any debugging before so I'm not clear what the results mean. Perhaps you were expecting that?
                    I don't know if the structure of the site is relevent. In case it is:
                    test/index.php is the home page of my blog displaying links to post.php (with which you are familiar). Hit a link from index.php to post 1 and post.php finds and displays post1 ..etc. This bit works fine and the debugging code you supplied:

                     echo "going to check for post_id<br>";
                    
                    if (preg_match("/^[0-9]+$/", $post_id)) {
                    	echo "post_id is set with ".$post_id;
                    	$sql = "SELECT post_id, title, post, DATE_FORMAT(postdate, '%e %b %Y at %H:%i') 
                        AS dateattime FROM posts
                    	WHERE post_id=$post_id LIMIT 1";
                    
                    //run this to make sure query is not empty
                    echo $sql."<br>";
                    
                    $result = mysql_query($sql) or die ("failed to get post for id $post_id<br>")
                    .mysql_error;
                    $myposts = mysql_fetch_array($result);
                    //overkill
                    echo "<pre>";
                    print_r($myposts);
                    echo "</pre>";
                    
                    } else {
                    	echo "post_id check failed, I got passed $post_id for the post_id";
                    	}

                    returns as follows when accessed from index. php to post .php (requesting post 7):

                    going to check for post_id
                    post_id is set with 7SELECT post_id, title, post, DATE_FORMAT(postdate, '%e %b %Y at %H:%i') AS dateattime FROM posts WHERE post_id=7 LIMIT 1

                    Array
                    (
                    [0] => 7
                    [post_id] => 7
                    [1] => debug
                    [title] => debug
                    [2] => testing posts.php
                    [post] => testing posts.php
                    [3] => 2 Aug 2007 at 00:14
                    [dateattime] => 2 Aug 2007 at 00:14
                    )

                    in addition to the relevent post, post.php displays a form to gather name email wwwsite and comments. This is the bit that goes awry. Hit the post comment button and your debug script returns:

                    going to check for post_id
                    post_id check failed, I got passed for the post_id
                    Notice: Undefined variable: myposts in /Library/WebServer/Documents/test/post.php on line 68
                    Notice: Undefined variable: myposts in /Library/WebServer/Documents/test/post.php on line 145
                    there is no post matching a post_id of .
                    Notice: Undefined variable: mycomments in /Library/WebServer/Documents/test/post.php on line 167

                    lines 68 - 73 read:

                    68 if ($myposts) {
                    69		$sql = "SELECT comment_id, name, website, comment FROM comments  
                    WHERE post_id=$post_id"; 71 $result3 = mysql_query($sql); 72 $mycomments = mysql_fetch_array($result3); 73 }

                    lines 145 -157:

                    145 if ($myposts) {//displays post
                    	146 do {
                    	147 	$post_id = $myposts["post_id"];
                    	148 	$title = $myposts["title"];
                    	149 	$post = format($myposts["post"]);
                    	150 	$dateattime = $myposts["dateattime"];
                    	151 	echo "<h3>$title</h3>\n";
                    	152 	echo "<div class='post'>\n $post \n</div>";
                    	153 	echo "<div class='right'>posted on $dateattime</div>";
                    	154  }   while ($myposts = mysql_fetch_array($result));
                           155  } else {
                    	156     echo"<p>there is no post matching a post_id of $post_id.</p>";
                            157 } 

                    and 167- 184:

                    167  if ($mycomments) {
                    	 168  echo "<dl>";
                    	 169  do {
                    	 170	$comment_id = $mycomments["comment_id"];
                    	 171	$name = $mycomments["name"];
                    	 172	$website = $mycomments["website"];
                    	 173	$comment = format($mycomments["comment"]);
                    	 174	if ($website !="") {
                    	 175		echo "<dt><a href='$website'>$name</a> wrote:</dt>\n";
                    	 176	} else {
                    	 177		echo "<dt>$name wrote:</dt>\n";
                    	 178	}
                    	 179	echo "<p><dd>$comment</dd></p>\n";
                    	 180    } while ($mycomments = mysql_fetch_array($result3));
                    	 181       echo "</dl>";
                    	 182    } 	else {
                    	 183	echo"<p class='central'>use the <em>add a comment</em> form to       
                    leave a comment.</p>"; 184 }

                    Sorry to be so long winded about this, Protato. I'm not sure what information is relevent and what isn't.
                    Many thanks again for your interest.
                    Cheers
                    David

                      The debugging here are just little bits to look at the information as it goes through your script. Like in your index which is pulling the posts ok, the echo statments show us what we'd expect.

                      going to check for post_id
                      post_id is set with 7
                      SELECT post_id, title, post, DATE_FORMAT(postdate, '%e %b %Y at %H:%i') AS dateattime FROM posts WHERE post_id=7 LIMIT 1
                      

                      It checked for the post_id, and let us know that it got it with 7, then we echoed the sql querry so we could look at it and make sure that's not the problem.

                      Now in the post php, we didn't get a confirmation from the echo statement that our post_id was set, which means the if(preg) failed. The line that says

                      post_id is set with
                      

                      Back pedeling to what NogDog said, your if statement that checks the post_id is failing. From the bits we made the script spit out it looks like the post_id is not getting set in the comments script. Does your index.php post to comments.php? Or do you do an include()? If you post/get to the comments file just add this before you check the post_id:

                      $post_id = $_POST['post_id'];
                      //of if you used get (comments.php?post_id=1)
                      $post_id = $_GET['post_id'];
                      

                      If you don't already have the post_id in a form, try adding a hidden field to your add comment form with the post_id. That way when a new comment is posted, this will include the post_id, and you can assign it using the little snip above. Let me know if that doesn't make sense.

                        Thanks Protato.
                        I understand what's going wrong now, but I'm not clear on how to correct it.
                        You ask:

                        Does your index.php post to comments.php? Or do you do use an include()?

                        I don't have comments.php. What I've done is seek to return to posts.php with a comment added when the post comment button is hit. The code I'm using for this is:

                         <form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>">
                        2 <input type="hidden" name="post_id" value="<?=$post_id ?>" />
                        //is  line 2 (above) the hidden field you refer to?
                        <input type="hidden" name="posttitle" value="<?=$title ?>" />
                        <h4>add a comment:</h4>
                        <?php
                        if (isset($message)) {
                        	echo "<p>".$_POST["message"]."</p>";
                        	}
                        	?>
                        <p>name: <br /><input name="name" type="text" /></p>
                        	<p>email: <br /><input name="email" type="text" /></p>
                        	<p>website: <br /><input name="website" type="text" /></p>
                        	<p>comment: <br /><textarea name="comment" cols="20" rows="10"></textarea></p>
                        	<p class="align"><input type="submit" name="postcomment" value="post comment" /></p>
                        	</form>

                        index.php links to posts.php using this code:

                         <?php
                        if($myposts) {
                        	echo "<ul id='nav1'>\n";
                        	do {
                        		$post_id = $myposts["post_id"];
                        		$title = $myposts["title"];
                        		$summary = $myposts["summary"];
                        	echo "<li><a href='post.php?post_id=$post_id' rel='bookmark'>$title</a></li>\n";
                        		} while ($myposts = mysql_fetch_array($result));
                        		echo "</ul>";
                        }
                        ?>

                        posts.php is supposed to link back to posts.php (with comment added) when the add comment form form has been completed and post comment button is hit. I don't have a link from index.php to (the non existant) comments.php - Perhaps herein lies my fundamental error?

                        If you don't already have the post_id in a form, try adding a hidden field to your add comment form with the post_id. That way when a new comment is posted, this will include the post_id

                        I'm not entiirely sure what post_id you refer to. I assume it's the hidden field one in the code above (line 2 of first php script above). But it might be this:

                        
                        1   <?php
                        2   if ($myposts) {//displays post
                        3     	do {
                        4     		$post_id = $myposts["post_id"];
                        5     		$title = $myposts["title"];
                        6     		$post = format($myposts["post"]);
                        7     		$dateattime = $myposts["dateattime"];
                        8     		echo "<h3>$title</h3>\n";
                        9			echo "<div class='post'>\n $post \n</div>";
                        10   	       echo "<div class='right'>posted on $dateattime</div>";
                        11   	}   while ($myposts = mysql_fetch_array($result));
                        12          } else {
                        13   	echo"<p>there is no post matching a post_id of $post_id.</p>";
                        14   }
                        15   ?>

                        which appears in the <div id ="content"> part of posts.php. It displays the $post_id when post.php is called from index.php, but is missing from the display following request from post comment button.
                        Finally, I see the sense in adding

                         $post_id = $_POST['post_id']; 
                        //of if you used get (comments.php?post_id=1) 
                        $post_id = $_GET['post_id']; 

                        but I've no idea where to put it!
                        I'm finding this journey fascinating and much appreciate your support.
                        Thank you
                        David

                          comments.php was an error on my part, I meant posts.php.

                          Take a look at the code for the add comment form;

                          <?
                          //look at <input type="hidden", it's got your post_id in there.
                          ?>
                          <form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>">
                          2 <input type="hidden" name="post_id" value="<?=$post_id ?>" />
                          //is  line 2 (above) the hidden field you refer to?
                          <input type="hidden" name="posttitle" value="<?=$title ?>" />
                          <h4>add a comment:</h4> 
                          
                          <?
                          //So at the top of posts.php, we add this little bit.  I think the problem is that your script is expecting the post_id to be set through register globals, which is turned off by default in newer versions of php.
                          $post_id = $_POST['post_id'];
                          ?>
                          

                            Here's the php code above the html tags of posts.php:

                             
                                <?php
                                 //open connection to database universal
                                 include("../db_connect.php");
                            
                             //get post_id from query string universal
                            6   $post_id = (isset($_REQUEST["post_id"]))?$_REQUEST["post_id"]:"";
                            7   //$post_id = $_POST['post_id'];
                            
                             echo "going to check for post_id<br>";
                            
                             if (preg_match("/^[0-9]+$/", $post_id)) {
                                 echo "post_id is set with ".$post_id;
                                 $sql = "SELECT post_id, title, post, DATE_FORMAT(postdate, '%e %b %Y at %H:%i') 
                                    AS dateattime FROM posts WHERE post_id=$post_id LIMIT 1";
                            
                            //run this to make sure query is not empty
                            echo $sql."<br>";
                            
                            $result = mysql_query($sql) or die ("failed to get post for id $post_id<br>")
                            .mysql_error;
                            $myposts = mysql_fetch_array($result);
                            //overkill
                            echo "<pre>";
                            print_r($myposts);
                            echo "</pre>";
                            
                            } else {
                            	echo "post_id check failed, I got passed $post_id for the post_id";
                            	}
                            
                            include("functions.php");
                            
                            //if comment has been submitted and post exists then add comment to database
                            if (isset($_POST["postcomment"]) != "") {
                            	$posttitle = addslashes(trim(strip_tags($_POST["posttitle"])));
                            	$name = addslashes(trim(strip_tags($_POST["name"])));
                            	$email = addslashes(trim(strip_tags($_POST["email"])));
                            	$website = addslashes(trim(strip_tags($_POST["website"])));
                            	$comment = addslashes(trim(strip_tags($_POST["comment"])));
                            
                            $sql = "INSERT INTO comments
                            	(post_id,name,email,website,comment)
                            	VALUES ('$post_id', '$name', '$email', '$website', '$comment')";
                            $result2 = mysql_query($sql);
                            if (!$result2) {
                            	$message = "failed to insert comment.";
                            } else {
                            	$message = "comment added.";
                            	$comment_id = mysql_insert_id();
                            
                            	//send self an email when a comment is successfully added (deleted for brevity)
                            
                            }
                            }
                            //pull comments from database
                            	if ($myposts) {
                            		$sql = "SELECT comment_id, name, website, comment FROM comments 
                                            WHERE post_id=$post_id";
                            		$result3 = mysql_query($sql);
                            		$mycomments = mysql_fetch_array($result3);
                            	}
                            ?>

                            You'll notice I've commented out line 7, which is the code snippet you just offered (I'm not sure that I put it in the right place). Running the script as above (with line 7 commented out) I get the expected result when linking from index.php to posts.php.
                            When I uncomment it and run the script from index.php to posts.php the return is:

                            Notice: Undefined index: post_id in /Library/WebServer/Documents/test/post.php on line 7
                            going to check for post_id
                            post_id check failed, I got passed for the post_id
                            Notice: Undefined variable: myposts in /Library/WebServer/Documents/test/post.php on line 67
                            Notice: Undefined variable: myposts in /Library/WebServer/Documents/test/post.php on line 144
                            Notice: Undefined variable: mycomments in /Library/WebServer/Documents/test/post.php on line 166

                            Line 7 is the now uncommented

                             $post_id = $_POST['post_id'];

                            and lines 67, 144 and 166 are :

                            67    if ($myposts) {
                            144   if($myposts) {
                            166   if($mycomments) {
                            

                            - the same snippets that threw up the original error messages.
                            I notice also that line 6:

                            $post_id = (isset($_REQUEST["post_id"]))?$_REQUEST["post_id"]:"";

                            seems to attempting a similar function to the newly introduced line 7 and wondered if this might be causing some conflict? Anyway, I hope the full code above the html tags is of help.
                            Incidentally, completing the add comments form and hitting post comment results in no change (apart from in the address bar of the browser).
                            Really admire your patience, Protato.
                            David

                              You had the code in the right place, the the little bit you had there is basically the same thing.

                              Put this right after line 7

                              //the post_id is going to be in the $_POST array, this will dump out all the elements in the array so we can see exactly what's in there and why it can't find what it needs.
                              echo "<pre>";
                              print_r($_POST);
                              echo "</pre>";
                              

                              Post the results here and we will continue from there. The post_id is still not getting assigned for some reason.

                                OK Protato
                                This code:

                                1    <?php
                                2    //open connection to database universal
                                3    include("../db_connect.php");
                                4
                                5    //get post_id from query string universal
                                6    $post_id = (isset($_REQUEST["post_id"]))?$_REQUEST["post_id"]:"";
                                7    $post_id = $_POST['post_id'];
                                8    echo "<pre>"; 
                                9    print_r($_POST); 
                                10  echo "</pre>"; 
                                11
                                12  echo "going to check for post_id<br>";
                                

                                where lines 8-10 are new, returns this:

                                Notice: Undefined index: post_id in /Library/WebServer/Documents/test/post.php on line 7

                                Array
                                (
                                )

                                going to check for post_id
                                post_id check failed, I got passed for the post_id
                                Notice: Undefined variable: myposts in /Library/WebServer/Documents/test/post.php on line 70
                                Notice: Undefined variable: myposts in /Library/WebServer/Documents/test/post.php on line 147
                                Notice: Undefined variable: mycomments in /Library/WebServer/Documents/test/post.php on line 169

                                Most of which is by now familiar, apart from the empty array.
                                I'm not sure whether or not the new line 7 replaces line 6 or whether to use both, so I tried the above twice - once with line 6 active and once with it commented out. There was no difference in what was returned.
                                Hope that casts some light for you.
                                David

                                  Assuming your form is using the "post" method, lines 6-7 should be merged into one line:

                                  $post_id = (isset($_POST["post_id"])) ? $_POST["post_id"] : "";
                                  

                                  If, instead, you are using the "get" method or "post_id" is coming through a URL query string in a link, then replace $POST with $GET.

                                  Generally, $REQUEST should not be used, replacing it with the more specific array so as to avoid confusion if requests with the same identifier come from multiple sources (post, get, and/or cookies), with which value shows up in $REQUEST being affected by the variables_order setting.

                                    I think it uses GET for viewing the first time, and POST after a comment is added.

                                    comment out line 7 ($post_id = $_POST['post_id') and run it again. We want to look at what it outputs when you submit a comment.

                                      Hello NogDog
                                      Replacing line 6 with

                                       $post_id = (isset($_POST["post_id"])) ? $_POST["post_id"] : ""; 

                                      and running it via link from index.php returns:

                                      Array
                                      (
                                      )

                                      going to check for post_id
                                      post_id check failed, I got passed for the post_id
                                      Notice: Undefined variable: myposts in /Library/WebServer/Documents/test/post.php on line 71
                                      Notice: Undefined variable: myposts in /Library/WebServer/Documents/test/post.php on line 148
                                      Notice: Undefined variable: mycomments in /Library/WebServer/Documents/test/post.php on line 170

                                      lines 71, 148 and 170 are the usual:

                                      71  if ($myposts) {
                                      148 if ($myposts) {
                                      170 if($mycomments) {
                                      

                                      Doing the same with:

                                       $post_id = (isset($_GET["post_id"])) ? $_GET["post_id"] : ""; 

                                      returns:

                                      Array
                                      (
                                      )

                                      going to check for post_id
                                      post_id is set with 7SELECT post_id, title, post, DATE_FORMAT(postdate, '%e %b %Y at %H:%i') AS dateattime FROM posts WHERE post_id=7 LIMIT 1

                                      Array
                                      (
                                      [0] => 7
                                      [post_id] => 7
                                      [1] => debug
                                      [title] => debug
                                      [2] => testing posts.php
                                      [post] => testing posts.php
                                      [3] => 2 Aug 2007 at 00:14
                                      [dateattime] => 2 Aug 2007 at 00:14
                                      )
                                      On attempting to submit a comment both return:

                                      Array
                                      (
                                      )

                                      going to check for post_id
                                      post_id check failed, I got passed for the post_id
                                      Notice: Undefined variable: myposts in /Library/WebServer/Documents/test/post.php on line 71
                                      Notice: Undefined variable: myposts in /Library/WebServer/Documents/test/post.php on line 148
                                      Notice: Undefined variable: mycomments in /Library/WebServer/Documents/test/post.php on line 170

                                      The code for the add a comment form is:

                                      <form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>">
                                      <input type="hidden" name="post_id" value="<?=$post_id ?>" />
                                      <input type="hidden" name="posttitle" value="<?=$title ?>" />
                                      <h4>add a comment:</h4>
                                      <?php
                                      if (isset($message)) {
                                      	echo "<p>".$_POST["message"]."</p>";
                                      	}
                                      	?>
                                      	<p>name: <br /><input name="name" type="text" /></p>
                                      	<p>email: <br /><input name="email" type="text" /></p>
                                      	<p>website: <br /><input name="website" type="text" /></p>
                                      	<p>comment: <br /><textarea name="comment" cols="20" rows="10"></textarea></p>
                                      	<p class="align"><input type="submit" name="postcomment" value="post comment" /></p>
                                      	</form>
                                      

                                      So I think this means I'm using the post method.
                                      Hope that's got meaning!
                                      Thanks for your input.

                                      Hello Protato
                                      With this code:

                                           <?php
                                           //open connection to database universal
                                           include("../db_connect.php");
                                      
                                       //get post_id from query string universal
                                      6   $post_id = (isset($_REQUEST["post_id"]))?$_REQUEST["post_id"]:"";
                                           //$post_id = $_POST['post_id'];
                                      
                                       echo "<pre>"; 
                                       print_r($_POST); 
                                       echo "</pre>"; 
                                      
                                       echo "going to check for post_id<br>"; 

                                      post.php?post_id=%3C?=$post_id%20?%3E&message=comment%20added. (posts.php after comment has been added and submitted) returns:

                                      Array
                                      (
                                      )

                                      going to check for post_id
                                      post_id check failed, I got passed for the post_id
                                      Notice: Undefined variable: myposts in /Library/WebServer/Documents/test/post.php on line 71
                                      Notice: Undefined variable: myposts in /Library/WebServer/Documents/test/post.php on line 148
                                      Notice: Undefined variable: mycomments in /Library/WebServer/Documents/test/post.php on line 170

                                      So that's the output after submitting a comment.
                                      Does it make sense?
                                      Thanks again for your help
                                      David

                                        David,

                                        Have you been able to identify where things are getting mucked up? Try putting

                                        echo "<pre>";
                                        print_r($_POST);
                                        print_r($_GET);
                                        echo "</pre>";
                                        

                                        at the top of your script and let us know the results for viewing a post, then adding a comment. If this doesn't spit out a result for adding a comment, something is probably wrong with your form that posts the comment.