query help...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

query help...

Post by pleigh »

i have code here

Code: Select all

$query = "SELECT DATE_FORMAT(commentdate, '%M %D, %Y - %l:%i %p'), username, comment, commentID FROM 
		comments WHERE postID='$id' ORDER BY commentdate DESC";
		$result = @mysql_query($query) or die(mysql_error());
		$num = mysql_num_rows($result);
		
		if ($num > 0)
		{
			$uname = $_SESSION['firstname'];
			//report edit
			$editquery = "SELECT postID, username, commentID FROM comments 
			WHERE postID='$id' AND username='$uname'";
    		$editresult = @mysql_query($editquery) or die(mysql_error());    
    		if ($row = mysql_fetch_assoc($editresult))
    		{
				$edit = '<a href="reportcommentedit.php?eid='.$_GET['pid'].'&cid='.$row['commentID'].'" class=under>EDIT</a>';
    		}
    		else
   			{
        		$edit = '&nbsp;';
    		}
this is to get both the eid and the cid from here to another page...it works...but the problem is that when there are 2 or more edit link, their cid is the same....so when i edit the the last comment, the it is changing the first comment, and not the current comment....i know there's something wrong about my query...can you help me guys?

thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is it the links (line 15) that aren't showing up correctly, or the edit bits on the following page?
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

thanks feyd...yes, line 15 is not showing correclty and the edit page...when, for example, i have 2 edit links, they both show the same cid, the value of the first comment is "apple", the 2nd is "banana", apple is the latest comment posted, when i click the edit of the 2nd comment, which is banana, the edit form loads and the value of the textarea is apple...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

well, you aren't looping over the results that are affected by that call, so I'd assume it's that.
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

for the complete script...

Code: Select all

<?
		$query = "SELECT DATE_FORMAT(commentdate, '%M %D, %Y - %l:%i %p'), username, comment, commentID FROM 
		comments WHERE postID='$id' ORDER BY commentdate DESC";
		$result = @mysql_query($query) or die(mysql_error());
		$num = mysql_num_rows($result);
		
		if ($num > 0)
		{
			$uname = $_SESSION['firstname'];
			//comment edit
			
			$editquery = "SELECT postID, username, commentID FROM comments 
			WHERE postID='$id' AND username='$uname'";
    		$editresult = @mysql_query($editquery) or die(mysql_error());    
    		if ($row = mysql_fetch_assoc($editresult))
    		{
				$edit = '<a href="reportcommentedit.php?eid='.$_GET['pid'].'&cid='.$row['commentID'].'" class=under>EDIT</a>';
    		}
    		else
   			{
        		$edit = '&nbsp;';
    		}
			
			//for comment
			if ($num <= 1)
			{
				$c = 'Comment';
			}
			else
			{
				$c = 'Comments';
			}
			echo "
			<tr valign=top class=tablehead align=center>
			<td width=15% class=tablehead><table><tr><td><font color=#ffffff><b/>Author</font></td></tr></table></td>
			<td width=85% class=tablehead><table><tr><td><font color=#ffffff><b/>$c</font></td></tr></table></td>
			</tr>";
			
			while($row = mysql_fetch_array($result, MYSQL_NUM))
			{
				$row[0] = '<font size=1>'.$row[0].'</font>';
				echo "			
				<tr valign=top>
				<td width=15% class=tableborder align=center><table><tr><td align=center>$row[1]<br>$row[0]</td></tr></table></td>
				<td width=85% class=tableborder><table><tr><td>
				<table width=100%>				
				<tr valign=top width=20%><td width=100%>".nl2br($row[2])."</td></tr>
				<tr valign=top width=20%><td width=100%>$edit</td></tr>
				</table>
				</td></tr></table></td>
				</tr>";
			}			
		}
		else
		{
			echo NULL;
		}
		?>
i don't know where the problem is...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you aren't looping over the results. You requested a single record, nothing more.
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

Code: Select all

while($row = mysql_fetch_array($result, MYSQL_NUM))
            {
                $row[0] = '<font size=1>'.$row[0].'</font>';
                echo "            
                <tr valign=top>
                <td width=15% class=tableborder align=center><table><tr><td align=center>$row[1]<br>$row[0]</td></tr></table></td>
                <td width=85% class=tableborder><table><tr><td>
                <table width=100%>                
                <tr valign=top width=20%><td width=100%>".nl2br($row[2])."</td></tr>
                <tr valign=top width=20%><td width=100%>$edit</td></tr>
                </table>
                </td></tr></table></td>
                </tr>";
            }
isn't this the loop youre looking??
Post Reply