Hello guys, I am doing a Friend system on my website, and I did the friend request, to accept, but the friends list isn't alright in some part of code because it isn't take the correct id, anyone can help me?

1.         <?php 
2.         
3.         $users = $_SESSION['username'];
4.         
5.         $connect = mysql_connect("localhost","root","") or die(mysql_error());
6.         mysql_select_db("website");
7.         
8.         $user = $_GET['u'];
9.         
10.              $sql =  mysql_query ("SELECT * FROM users");   
11.              
12.              $result = mysql_fetch_assoc($sql);
13.              
14.              
15.              $id3 = $result['id'];
16.             
17.         
18.         $selusers = mysql_query("SELECT * FROM u_friends WHERE friend='$users' OR username='$users'");
19.             
20.             $row = mysql_num_rows($selusers);
21.         
22.         if($row!=0) {
23.                 echo "<h1>Amigos</h1>";
24.                 echo "<table align='center'>";
25.             while($rows = mysql_fetch_assoc($selusers)) {
26.                 $id2 = $rows['id'];
27.         
28.                 $read = $rows['accepted'];
29.                 
30.             
31.           
32.                 if($read==1) 
33.                     if(isset($_SESSION['username'])&&$_SESSION['username']!=($rows['username'])) {
34.                         
35.         
36.                 echo "<tr>";
37.                 echo "<td>";
38.                 echo "<a href='index.php?id=profile&u=".$id3."'>".$rows['username']."</a>";   
39.                 echo "</td>";
40.                 echo "</tr>";
41.                     } else if(isset($_SESSION['username'])&&$_SESSION['username']==($rows['username'])) {
42.                        echo "<tr>";
43.                 echo "<td>";
44.                 echo "<a href='index.php?id=profile&u=".$id3."'>".$rows['friend']."</a>"; 
45.                 echo "</td>";
46.                 echo "</tr>";    
47.                     }  else {
48.                   echo "<table><tr><td>Não há utilizadores!</td></tr></table>";       
49.                 }
50.             echo "</table>";
51.         
52.             }
53.                     
54.         }
55.          else {
56.           echo "<table><tr><td>Não há utilizadores!</td></tr></table>";   
57.         }
58.         
59.         
60.         ?>

Anyone can help me please?When it says $id3, that's the problem

The error is when I put the echo "<a href='index.php?id=profile&u=".$id3."'>".$rows['friend']."</a>";

Member Avatar for diafol

show your table structures

Here they are:

1. TABLE `users` (
2. `id`, int(11) NOT NULL AUTO_INCREMENT,
3. `username` varchar(200) NOT NULL,
4. `password` varchar(30) NOT NULL,
5. `email` varchar(500) NOT NULL,
6. `repeatemail` varchar(500) NOT NULL,
7. `country` varchar(200) NOT NULL,
8. `gender` varchar(60) NOT NULL,
9. `birthday` varchar(60) NOT NULL,
10. `date` date NOT NULL,
11. `random` varchar(20) NOT NULL
12. `activated` tinyint(1) NOT NULL DEFAULT
13. PRIMARY KEY(`id`)
14. )
15. 
16. TABLE `u_friends` (
17. `id` int(11) NOT NULL AUTO_INCREMENT,
18. `username` varchar(200) NOT NULL,
19. `friend` varchar(200) NOT NULL,
20. `date` date NOT NULL,
21. PRIMARY KEY(`id`)
22. )
23. 

I think $id3 holds the ID of the user that is logged in. It's $id2 that holds the value of the friend ID which is what you are trying to echo to the screen at that point. I think line 44 in your sample code should be:

echo "<a href='index.php?id=profile&u=".$id2."'>".$rows['friend']."</a>";
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.