-1

I have created a members.php page that connects to a database table. The table has the following fields: id, username, profile image.

The following PHP code displays the profile image for each user in rows of 6 and allows each image to be clickable.

<?php
            // The code below will display the current users who have created accounts with:  **datemeafterdark.com**
            $result=mysql_query("SELECT * FROM profile_aboutyou");
            $row1 = mysql_fetch_assoc($result);
            
            $id = $row1["id"];
            $_SESSION['id'] = $id;
            
            $profileimagepath = $row1["profileimagepath"];
            $_SESSION['profileimagepath'] = $profileimagepath;
            
            
              $count = 0;
                  while($dispImg=mysql_fetch_array($result))
                      {
                          if($count==6) //6 images per row
                              {
                                  print "</tr>";
                                  $count = 0;
                              }
                          if($count==0)
                              print "<tr>";
                                  print "<td>";
        ?>                          
                                    <center>
                                        <a href="viewmemberprofile.php"><img src="<?php echo $dispImg['profileimagepath'];?>" width="85px;" height="85px;"></a>
                                    </center>
                           <?php
                                        $count++;
                                  print "</td>";
                      }           
                                  if($count>0)
                              print "</tr>";
                              
                           ?>

This is all great, however, when I click on the image that loads it re-directs me to: viewmemberprofile.php which is what it is supposed to do. But it always displays the same image with the same id value (i.e.) 150 no matter which image I click. What I would like to have happened is. If I click on an image with id 155 etc... it will display content for that image data field not consistently the same image data regardless of which image I click.

Your help and guidance would be greatly appreciated. Thank you in advance.

One thing that I forgot to mention is that I do use sessions so... when I am re-directed to the viewmemberprofile.php page I use the following code to aide in getting the data that I need from the table.

          <?php
            $id = $_SESSION['id'];
            echo($id);
          ?>
          <?php
            echo('<br>');
          ?>
          <?php
            $profileimagepath = $_SESSION['profileimagepath'];
          ?>
          <img src="<?php echo($profileimagepath);?>" width="50px;" height="50px;">

I have yet to impliment the suggested solution.

  • (1) Make sure you have put session_start() at the start of the PHP scripts (2) mysql_query is deprecated, please use either mysqli or PDO – Ken Lee Nov 25 '21 at 05:33
  • 1. This should not be done using the session in the first place. 2. _"The following PHP code displays the profile image for each user"_ - no it doesn't; you are fetching the first record _before_ the while loop already, but never output the data of that record - so the first user gets skipped. – CBroe Nov 25 '21 at 07:55

1 Answers1

0

You need to pass the ID of the row to viewmemberprofile.php, e.g.:

<a href="viewmemberprofile.php?id=<?= $dispImg['profileimagepath'] ?>">

And viewmemberprofile.php needs to select that row from the DB:

SELECT * FROM profile_aboutyou WHERE id = $_GET['id']

The above SQL statement is pseudo-code; you need to write actual code to accomplish what it is describing, preferably using parameterized queries.

kmoser
  • 8,780
  • 3
  • 24
  • 40