Jump to content

[SOLVED] Problem with correct code to view output


Recommended Posts

Hi all,

 

I have the below which is meant to display the replies to a forum post only if the users userlevel is greater than 3, or the person viewing the replies is the same person that posted the question. The problem I have is that the user that is logged in is the user who posted the original question and is user level 5 but the posts are not displayed. Is this a problem with my or statement? Does it need to be something like an and/or if such a thing exists?

 

Cheers:

 

<?php
session_start()
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="Description" content="" />
<meta name="Keywords" content="" />
<meta name="Robots" content="index,follow" />
<link rel="stylesheet" href="images/CoolWater.css" type="text/css" />
<title>Project</title>
</head>
<body>
<div id="wrap">
<?php include('include/header.php'); ?>
<div id="content-wrap">
<div id="main">
<?php
include('include/dbconnect.php');
if (!isset($_SESSION['logged'])) 
	{
    	header("Location: login.php");
	}
		$query="SELECT user_id FROM forum_question WHERE username = '" . $_SESSION['username'] ."'";
		$result=mysql_query($query) or die ("oh dear   " .mysql_error()) ;
		while($rows=mysql_fetch_array($result))
			{
			$_SESSION['user_id'] = $rows['user_id'];
			}
				$id=$_GET['id'];
				$sql="SELECT * FROM forum_question WHERE id='$id'";
				$result=mysql_query($sql) or die ("oh dear   " .mysql_error()) ;
				while($rows=mysql_fetch_array($result))
			{
?>
    
    
    

<h1><?php echo $rows['title'];?></h1>
<table>
<tr>
	<th>System Info: 
        </th>
    		<td>Operating System: <?php echo $rows['os'] ;?>,<br />
			Amount of Ram: <?php echo $rows['ram'] ;?>,<br />
			Harddrive Type &amp Size: <?php echo $rows['harddrive'] ;?>, <br />
		Graphics Card: <?php echo $rows['graphics'] ;?>
        	</td>
</tr>
<tr>
	<th>Topic: 
        </th>
        	<td> <?php echo $rows['title'] ;?> 
            </td>
</tr>

     <tr>
	<th>Posted By: </th>
        <td> <?php echo $rows['username'] ;?> 
        </td>
</tr>
            
<tr>
		<th>Posted On: </th>
            <td> <?php echo $rows['datetime'] ;?> 
            </td>
</tr>

<tr>
		<th>Description: 
            </th>
            <td> <?php echo $rows['detail'];?> 
            </td>
</tr>

	<?php }?>
        
        
		<tr>
		<th>Download DxDIAG:
            </th>
		<?php 
			$query = "SELECT u_id, name
			FROM upload 
			WHERE post_no = '$id'";
			$result = mysql_query($query) or die('Error, query failed');


			if(mysql_num_rows($result) == 0)
				{
				echo "No DxDIAG Exists <br>";
				}
			else
				{
				while(list($u_id, $name) = mysql_fetch_array($result))

				?>
				<td><a href="forum/download.php?id=<?php echo $id;?>">Download</a></td><br>
				</table>
					<?php

						$userID = $_SESSION['user_id'];

$result = mysql_query("SELECT * FROM users_info WHERE user_id='$userID' LIMIT 1") or die(mysql_error()); 
$row = mysql_fetch_array($result);

					?>
                        
                        <?php

					   	if($rows['userlevel'] > 3 || $rows['user_id'] == $userID) 
							{
								$sql2="SELECT * 
								FROM forum_answer 
								WHERE question_id='$id'";
								$result2=mysql_query($sql2) or die ("oh dear   " .mysql_error()) ;
								while($rows=mysql_fetch_array($result2))
									{
					?>
                        					<table width="95%" border="0">
                        					<hr> <h3>Reply Number  <span style="color:#F00"><?php echo $rows['a_id'] ;?></span></h3>
  												<tr>
    												<th width="20%">Posted On:
                                                    </th>
    												<td><span style="color:#0F7"><?php echo $rows['a_datetime'] ;?></span>
                                                    </td>
    											</tr>
  												<tr>
    													<th width="20%"><?php echo $rows['a_username'];?>'s Reply:</th>
    													<td><span style="color:#00F"> <?php echo $rows['a_answer']; ?></span>
                                                        </td>
  												</tr>
										</table>
                                            
                          <?php }?>                  
                                            
<?php
$query = "SELECT username
FROM forum_question";
$result = mysql_query($query, $conn) or die(mysql_error($conn));
$row = mysql_fetch_array($result);
extract($row);
mysql_free_result($result);

	if ($username == $_SESSION['username'])
		{
?>
          		<form method="post" action="forum/forums/is_solved.php">
                <input type="submit" class="button" value="This Fixed It" />
                </form>
<?php 
		}
?>


<?php 
													} 
															}
?>

                                     		

						<?php



							$sql3="SELECT view 
							FROM forum_question 
							WHERE id='$id'";
							$result3=mysql_query($sql3) or die ("oh dear   " .mysql_error()) ;
							$rows=mysql_fetch_array($result3);
							$view=$rows['view'];

							// if have no counter value set counter = 1
							if(empty($view))
								{
							$view=1;
							$sql4="INSERT INTO forum_question(view)
							VALUES('$view') 
							WHERE id='$id'";
							$result4=mysql_query($sql4)or die ("oh dear   " .mysql_error()) ;
								}

								// count more value
								$addview=$view+1;
								$sql5="UPDATE forum_question
								SET view='$addview' 
								WHERE id='$id'";
								$result5=mysql_query($sql5)or die ("oh dear   " .mysql_error()) ;



?> 
  



					<form method="post" action="forum/add_answer.php?id=<?php echo $id ; ?>">			
                        <label><?php echo $_SESSION['username']; ?>'s Reply</label>
					<textarea rows="5" cols="5" name="answer" id="answer"></textarea>
					<input name="id" type="hidden" value="<?php echo $id ;?>" />
					<input class="button" type="submit" />		
					</form>	
                        
                        <table>
                <tr>
			<th><a href="solutions.php">Back</a></th>
               
			</tr>
                </table>
</div>		
<?php
if (isset($_SESSION['logged']) && $_SESSION['logged'] == 1) 

{ include('include/sidebar_1.php'); }


                                     
else

{ include('include/sidebar_0.php'); }			
?>
    <!-- content-wrap ends here -->	
    </div>				
<?php include('include/footer.php');?>
<!-- wrap ends here -->
</div>

</body>
</html>

The problem is your while loop. What your while loop does is run until $mysql_fetch_array doesn't return any more rows. That means that it tries see if there are any more rows, but it returns false, which gets stored in $rows. so when you access it in your if statement, it is false. also note that here:

$result = mysql_query("SELECT * FROM users_info WHERE user_id='$userID' LIMIT 1") or die(mysql_error()); 
$row = mysql_fetch_array($result);

you are using the var $row

 

and in your if statement:

if($rows['userlevel'] > 3 || $rows['user_id'] == $userID)

you are using $rows. if this is what you mean to do, I suggest instead of doing this

while($rows=mysql_fetch_array($result))
			{
			$_SESSION['user_id'] = $rows['user_id'];
			}

you simply assign rows, like so

$rows = mysql_fetch_array($result);
$_SESSION['user_id'] = $rows['user_id'];

 

by the way, you also use $rows here

$id=$_GET['id'];
				$sql="SELECT * FROM forum_question WHERE id='$id'";
				$result=mysql_query($sql) or die ("oh dear   " .mysql_error()) ;
				while($rows=mysql_fetch_array($result))

 

I would suggest using different variable names, especially for different queries, as when you trying to trace back the error, it becomes difficult if you have one variabled used in multiple places

Thanks for the fantastic post mikesta, it was really helpful. I have managed to get it working by using this:

 

<?php
session_start()
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="Description" content="" />
<meta name="Keywords" content="" />
<meta name="Robots" content="index,follow" />
<link rel="stylesheet" href="images/CoolWater.css" type="text/css" />
<title>Project</title>
</head>
<body>
<div id="wrap">
<?php include('include/header.php'); ?>
<div id="content-wrap">
<div id="main">
<?php
$id=$_GET['id'];
include('include/dbconnect.php');
if (!isset($_SESSION['logged'])) 
	{
    	header("Location: login.php");
	}
		?>
            
           		<?php $query1="SELECT * FROM forum_question WHERE id='$id'";
               $result1=mysql_query($query1) or die ("oh dear   " .mysql_error()) ;
               $rows = mysql_fetch_array($result1);
		   $_SESSION['os'] = $rows['os'];
		   $_SESSION['ram'] = $rows['ram'];
		   $_SESSION['harddrive'] = $rows['harddrive'];
		   $_SESSION['graphics'] = $rows['graphics'];
		   $_SESSION['title'] = $rows['title'];
		   $_SESSION['p_username'] = $rows['username'];
		   $_SESSION['datetime'] = $rows['datetime'];
		   $_SESSION['detail'] = $rows['detail'];
		   $_SESSION['user_id'] = $rows['user_id'];
		   ?>
               
               <table>
               <tr>
               <th>Users System Info</th>
               <td>	Operating System: <?php echo $_SESSION['os'];?><br />
               		Amount of Ram:  <?php echo $_SESSION['ram'];?><br />
                    Harddrive Specs:  <?php echo $_SESSION['harddrive'];?><br />
                    Graphics Card:  <?php echo $_SESSION['graphics'];?><br />
              	</td>
                </tr>
                
                <tr>
                <th>Topic</th>
                <td><?php echo $_SESSION['title']; ?>
                </td>
                </tr>
                
                <tr>
                <th>Poster</th>
                <td><?php echo $_SESSION['p_username']; ?>
                </td>
                </tr>
                
                <tr>
                <th>Posted On</th>
                <td><?php echo $_SESSION['title']; ?>
                </td>
                </tr>
                
                <tr>
                <th>Problem Details</th>
                <td><?php echo $_SESSION['detail']; ?>
                </td>
                </tr>
                
                <tr>
                <th>DxDIAG Download</th>
                
                		<?php 
            				$query2 = "SELECT u_id, name FROM upload WHERE post_no = '$id'";
            				$result2 = mysql_query($query2) or die('Error, query failed');
            				if(mysql_num_rows($result2) == 0)
               					{
               					echo "No DxDIAG Exists <br>";
               					}
            				else
               					{
               				while(list($u_id, $name) = mysql_fetch_array($result2))
					?>
			<td><a href="forum/download.php?id=<?php echo $id;?>">Download</a></td>
                </tr>
   	  </table>
                
<?php
//define variable for post shows
$logged_user =$_SESSION['username'];
$poster = $_SESSION['p_username'];
$poster_user_id = $_SESSION['user_id'];
//end

$query3="SELECT userlevel,user_id FROM users_info WHERE user_name = '$logged_user'";
$result3=mysql_query($query3) or die ("oh dear   " .mysql_error()) ;
    $rows3 = mysql_fetch_array($result3);
$_SESSION['userlevel'] = $rows3['userlevel'];


//define userlevel variable
$userlevel = $_SESSION['userlevel']
?>

	<?php
	if($userlevel > 3 || $poster == $logged_user)
	{
		   $query4="SELECT * FROM forum_answer WHERE question_id='$id'";
               $result4=mysql_query($query4) or die ("oh dear   " .mysql_error()) ;
               while($rows4=mysql_fetch_array($result4))
										  {
		   $_SESSION['a_id'] = $rows4['a_id'];
		   $_SESSION['a_datetime'] = $rows4['a_datetime'];
		   $_SESSION['a_username'] = $rows4['a_username'];
		   $_SESSION['a_answer'] = $rows4['a_answer'];
	?>
  				<table>
                    <hr /><h3>Reply Number:  <span style="color:#F00"><?php echo $_SESSION['a_id']; ?></span></h3>
                    
                    <tr>
                    <th width="15%">Posted On: </th>
                    <td><?php echo $_SESSION['a_datetime'] ; ?>
                    </td>
                    </tr>
                    
                    <tr>
                    <th width="15%">Posted By: </th>
                    <td><?php echo $_SESSION['a_username'] ; ?>
                    </td>
                    </tr>
                    
                    <tr>
                    <th width="15%"><?php echo $_SESSION['a_username']; ?>'s Reply:</th>
                    <td><?php echo $_SESSION['a_answer']; ?>
                    </td>
                    </tr>
                    
                    <tr>
                    <td width="20%">
                    
                 	<?php
						if($poster == $logged_user)
						{
				?>
        						<form method="post" action="forum/forums/is_solved.php">
                				<input type="submit" class="button" value="This Fixed It" />
                				</form>
                    <?php
						}
				?>

				</td>
    				</tr>
                    </table>
                    <?php }}} ?>
                    				<?php

									if($userlevel > 3 || $poster == $logged_user)
										{
								?>
										<form method="post" action="forum/add_answer.php?id=<?php echo $id ; ?>">         
                        					<label><?php echo $_SESSION['username']; ?>'s Reply</label>
                  							<textarea rows="5" cols="5" name="answer" id="answer"></textarea>
                  							<input name="id" type="hidden" value="<?php echo $id ;?>" />
                  							<input class="button" type="submit" />      
 									 	</form>   
                                      <?php
										}
								?>
                       
                  	<table>
                	<tr>
            		<th><a href="solutions.php">Back</a></th>
               		</tr>
                	</table>
                     <?php
                        
                           

                        $query5="SELECT view 
                        FROM forum_question 
                        WHERE id='$id'";
                        $result5=mysql_query($query5) or die ("oh dear   " .mysql_error()) ;
                        $rows=mysql_fetch_array($result5);
                        $view=$rows['view'];

                        // if have no counter value set counter = 1
                        if(empty($view))
                           {
                        $view=1;
                        $sql4="INSERT INTO forum_question(view)
                        VALUES('$view') 
                        WHERE id='$id'";
                        $result4=mysql_query($sql4)or die ("oh dear   " .mysql_error()) ;
                           }

                           // count more value
                           $addview=$view+1;
                           $sql5="UPDATE forum_question
                           SET view='$addview' 
                           WHERE id='$id'";
                           $result5=mysql_query($sql5)or die ("oh dear   " .mysql_error()) ;

               

?> 
                    </div>
                        	<?php
							if (isset($_SESSION['logged']) && $_SESSION['logged'] == 1) 
								{ 
									include('include/sidebar_1.php'); 
								}

							else
								{ 
									include('include/sidebar_0.php'); 
								}			
						?>
   	
    </div>				
<?php include('include/footer.php');?>
</div>
</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.