I need help with a dynamic link on my web page. This is a site where accommodation establishments register their details and upload a photo.

Here is the problem I have:
I have a page listing all users (user_list.php) where I retrieve details (acc_name, address, photo) of registered users from mysql database. I also have a dynamic link to a page (profile.php) where details of user is listed with a photo.

Everything works fine but when I click the link (first click on link) to the specific users details (profile.php) it displays fine. However, when I go back to my user list (user_list.php) and click the link of another registered user (profile.php) the profile of the previous user are redisplayed. I have reset/cleared all my browsing history but still have the same problem. Below is the code for the link to profile.php in my user_list php page.

<td class='tduser' width=15% align='center' valign='middle'><a href='profile.php?id=".$row['id']."'>".$row['acc_name']."</td>

I am querying the database correctly? Please provide me with a solution to this problem. I am missing something and cannot get around to solving it.
Below find the full code for my user_list.php.

<?php
include 'dbc.php';
include 'includes/title.inc.php';
page_protect();
$member = @$_GET['member'];
/******************** ADD ME TO SHOW USERS AVATAR ****************************/
$img_url_query = mysql_query("select avatar_url from users where id = '$_SESSION[user_id]'");
while($img_url_settings = mysql_fetch_array($img_url_query)) {
$img_url = $img_url_settings['avatar_url'];
}
/*****************************************************************************/

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/default.css" type="text/css" media="screen"/>
<title>Places To Stay In The Eastern Cape<?php if (isset($title)) {echo "&#8212;{$title}";} ?></title>

</head>

<body>
<div id="wrapper">
<div id="topinfobar">
<p><span class="strapline">Magazine Shelf Life: </span>June - August 2010: <span class="style1">3 Months</span></p>
</div>

  <div id="topbanner1"></div>
   <div id="topbanner2"></div>
    <div id="topbanner3"></div>
     <div id="topbanner4"></div>
  
  <div id="cornerbox"></div>
  
  <div id="starlogo"></div>
  
  <div id="tourinfobar">
  <p class="intro">Accommodation Directory to the Eastern Cape's nine tourism routes:</p>
  <p>Tsitsikamma Route  |  Kouga Route  | Sunshine Coast | Frontier Country             
| Friendly N6 <br/> 
Karoo Heartland  | Greater Addo Route | Amatola Mountain Escape | Wildcoast/Cintsa</p>
  
  </div>
  
  <div id="info1"></div>
  
  <div id="topnavbar">
  <div id="topnav">
<?php include('includes/menu.inc.php'); ?>
</div>
  
  </div>
   <div id="register">
  <table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
   
    <tr>
      <td valign="top"><?php 
/*********************** MYACCOUNT MENU ****************************
This code shows my account menu only to logged in users. 
Copy this code till END and place it in a new html or php where
you want to show myaccount options. This is only visible to logged in users
*******************************************************************/
if (isset($_SESSION['user_id'])) {?>
        <div class="myaccount">
            <p><strong>My Account</strong></p>     
            <a href="myaccount.php">My Account</a><br />
            <a href="register.php">Register Details </a><br />
            <a href="imageupload.php">Upload Picture </a><br />
            <a href="profile.php">User Profile </a><br />
            <a href="mysettings.php">Update Details</a><br />
            <a href="list_users.php">View Users</a><br />
            <a href="logout.php">Logout </a>
           
          </div>
        <?php } 
/*******************************END**************************/
?>
          <p>&nbsp; </p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p></td>
        <td><?php

//check if the starting row variable was passed in the URL or not
if(!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {

//we give the value of the starting row to 0 because nothing was found in URL
$startrow = 0;

//otherwise we take the value from the URL
} else {
$startrow = (int)$_GET['startrow'];
}

$result = mysql_query("SELECT * FROM users LIMIT $startrow, 10") or die(mysql_error());
while($row = mysql_fetch_array($result)) { 

echo 
"<table class='tableuser' bgcolor='#FFDB94' cellspacing='0' cellpadding='0' border='0'>
<tr>
<td class='tduser' width='10%' align='center' valign='middle' bgcolor='#FFCC66'><p><img src=\"images/avatars/{$row['avatar_url']}\"$avatar_url\" width='100' border='1' /></p></td>
<td class='tduser' width='20%' align='center' valign='middle'><p>".$row['acc_name']."</p></td>
<td class='tduser'width='20%' align='center' valign='middle'><p>".$row['address']."</p></td>
<td class='tduser' width=15% align='center' valign='middle'><a href='profile.php?id=".$row['id']."'>".$row['acc_name']."</td>
</tr>
</table>";
}
?>
<div id="pagination">
  <?php

//now this is the link
echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+10).'"> Next >> </a>';

$prev = $startrow - 10;

//only print a Previous link if a next was clicked
if ($prev >= 0)
echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.$prev.'">|  << Previous</a>';
    ?>
</div>
</td>
       
<h3 class="titlehdr">List Users</h3>
      <p>Here you can view all registered users.</p><br />
      </tr>
 </table>
 </div>
  <div id="infostrip">
  <p>Information directory to the best accommodation establishments available in the eastern cape's nine tourism routes</p>
</div>

  <!-- start of bottom navigation -->
  <?php include('includes/footer_menu.inc.php'); ?>
  <!-- end of bottom navigation -->

<!-- start footer -->
<?php include('includes/footer.inc.php'); ?>
<!-- end footer -->
</div>

</body>
</html>

... where id = '$_SESSION[user_id]'") are you updating $_SESSION[user_id] to that of the one you selected/click on in profile.php?

No. I am not updating specfic user details. The link to profile.php will get the current saved details from database of user. User details get displayed when i click link. The problem is that the same user details show when click on link for different users in my user_list.php page. E.G: The profile for user1 gets display for user2 when i click link for profile of user2. The same for others when i click their links to view their profiles. User1 profile keeps getting displayed.

currently in user_list.php (line 95) you have SELECT * FROM users LIMIT $startrow, 10 . If in fact your users table has a field named `id`, then focus your efforts in profile.php since user_list.php seems fine. In profile.php you should be retrieving the user id by issuing $_GET['id'] NOT $_POST['id']

hielo. can you provide an example or link to a tutorial.

can you post your code to profile.php?

Hielo, Herewith my code for my profile.php page. Once again thank you for taking the time to look at this problem for me.

<?php
include 'dbc.php';
include 'includes/title.inc.php';
page_protect();

//retrieve the user's info
$q = "SELECT * FROM users where id='$_SESSION[user_id]'" ;
$res_q = mysql_query($q);
if(!$res_q){
echo mysql_error();
}else{
$row = mysql_fetch_assoc($res_q);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<title>Places To Stay In The Eastern Cape<?php if (isset($title)) {echo "&#8212;{$title}";} ?></title>
<script language="JavaScript" type="text/javascript" src="javascripts/jquery-1.3.2.min.js"></script>
<script language="JavaScript" type="text/javascript" src="javascripts/jquery.validate.js"></script>
<script language="javascript" type="text/javascript" src="javascripts/textcount.js"></script>
  <script>
  $(document).ready(function(){
    $("#myform").validate();
	 $("#pform").validate();
  });
  </script>
</head>

<body>
<div id="wrapper">
<div id="topinfobar">
<p><span class="strapline">Magazine Shelf Life: </span>June - August 2010: <span class="style1">3 Months</span></p>
</div>

  <div id="topbanner1"></div>
   <div id="topbanner2"></div>
    <div id="topbanner3"></div>
     <div id="topbanner4"></div>
  
  <div id="cornerbox"></div>
  
  <div id="starlogo"></div>
  
  <div id="tourinfobar">
  <p class="intro">Accommodation Directory to the Eastern Cape's nine tourism routes:</p>
  <p>Tsitsikamma Route  |  Kouga Route  | Sunshine Coast | Frontier Country             
| Friendly N6 <br/> 
Karoo Heartland  | Greater Addo Route | Amatola Mountain Escape | Wildcoast/Cintsa</p>
  
  </div>
  
  <div id="info1"></div>
  
  <div id="topnavbar">
  <div id="topnav">
<?php include('includes/menu.inc.php'); ?>
</div>
  
  </div>
  <div id="user_register">
   <table width="100%" border="0" cellspacing="0" cellpadding="0" class="main">
<tr>
      <td>&nbsp;</td>
    </tr>
  <tr>
      <td valign="top"><?php 
/*********************** MYACCOUNT MENU ****************************
This code shows my account menu only to logged in users. 
Copy this code till END and place it in a new html or php where
you want to show myaccount options. This is only visible to logged in users
*******************************************************************/
if (isset($_SESSION['user_id'])) {?>
        <div id="menu">
            <p><strong>My Account</strong></p> 
            <ul>     
          <li><a href="myaccount.php">My Account</a></li>
            <li><a href="register.php">Register Details </a></li>
            <li><a href="imageupload.php">Upload Picture </a></li>
            <li><a href="profile.php">User Profile </a></li>
            <li><a href="mysettings.php">Update Details</a></li>
            <li><a href="list_users.php">View Users</a></li>
            <li><a href="logout.php">Logout </a></li>
          </ul>
          </div>
        <?php } 
/*******************************END**************************/
?>
          <p>&nbsp; </p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p></td>
    <td colspan="3" valign="top"><h3 class="titlehdr">User Profile</h3>
    
    <p>
            <?php	
      if (isset($_GET['msg'])) {
	  $message = urlencode($_GET['msg']);
	  echo "<div class=\"msg\">$message</div>";
	  }
	  ?>
          </p>
    
    <p>Here you can view your registered profile. Use the links at the bottom of this page should you wish to register a new account, upload a picture or update your details.</p>   
    <table width="80%" border="0" cellspacing="3" cellpadding="0">
    <tr>
    <td class="tduser"><h1>Date Registered:</h1><?php echo $row['date_registered'];?></td>
    <td rowspan="4" class="tdphoto" valign="top"><p>Profile Photo:</p><br /> <?php echo "<img src=\"images/avatars/{$row['avatar_url']}\"$avatar_url\"  width='280' height='220' align='top' />"; ?></td>
  </tr>
  <tr>
    <td class="tduser"><h1>Acc Name:</h1><?php echo $row['acc_name'];?></td>
    </tr>
  <tr>
    <td class="tduser"><h1>Address:</h1><?php echo $row['address'];?></td>
  </tr>
  <tr>
    <td class="tduser"><h1>Contact:</h1><?php echo $row['contact'];?></td>
  </tr>
  <tr>
    <td class="tduser"><h1>Phone:</h1><?php echo $row['phone'];?></td>
  </tr>
  <tr>
    <td class="tduser" ><h1>Fax:</h1><?php echo $row['fax'];?></td>
     <td class="tduser"><h1>Tourism Route:</h1><?php echo $row['tourism_route'];?></td>
  </tr>
  <tr>
    <td class="tduser"><h1>Email:</h1><?php echo $row['user_email'];?></td>
     <td class="tduser"><h1>Acc Category:</h1><?php echo $row['acc_category'];?></td>
  </tr>
  <tr>
   <td class="tduser"><h1>Website:</h1><?php echo $row['website'];?></td>
    <td class="tduser"><h1>Acc Tariffs:</h1><?php echo $row['acc_tariffs'];?></td>
  </tr>
  <tr>
    <td class="tdbtm" colspan="3"><p>Accommodation Profile</p></td>
  </tr>
  <td></td>
  <tr>
    <td colspan="3"><p><?php echo $row['acc_profile'];?></p></td>
  </tr>
  
  <tr>
    <td class="tdbtm" colspan="3"><p>Accommodation Location</p></td>
  </tr>
  <td></td>
  <tr>
    <td colspan="3"><p><?php echo $row['acc_location'];?></p></td>
  </tr>
  
  <tr>
    <td class="tdbtm" colspan="3"><p>Accommodation Directions</p></td>
  </tr>
  <td></td>
  <tr>
    <td colspan="3"><p><?php echo $row['acc_directions'];?></p></td>
  </tr>
</table>
</td>
</tr>
</table>

  </div>
  <div id="infostrip">
  <p>Information directory to the best accommodation establishments available in the eastern cape's nine tourism routes</p>
</div>

  <!-- start of bottom navigation -->
  <?php include('includes/footer_menu.inc.php'); ?>
  <!-- end of bottom navigation -->

<!-- start footer -->
<?php include('includes/footer.inc.php'); ?>
<!-- end footer -->
</div>

</body>
</html>

instead of: $q = "SELECT * FROM users where id='$_SESSION[user_id]'" ; try: $q = sprintf("SELECT * FROM users where id='%s' LIMIT 1", mysql_real_escape_string($_GET['id']) ;

hielo. i tried your suggestion but i get a pass error. any reason why?

i get a pass error

Is this error reported by your server or is it some editor that you are using?

On another note, I missed closing parenthesis at the end of that line:

$q = sprintf("SELECT * FROM users where id='%s' LIMIT 1", mysql_real_escape_string($_GET['id']) ) ;

Hielo, apologies for late reply. Your suggestions worked for dynamic link problem i had. Appreciate your help. Will mark this post as solved. Thanks

You are welcome.

Regards,
Hielo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.