Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the problem is it print out the friends names as a numbers on this line of code
//
echo "<a href='profile?id=". $row['friend'] ."'>".$row['friend']."</a>";


What I have tried:

this is my full code which i have tried

$stmt = $conn->prepare("SELECT `friends`.*, users.username AS username FROM `friends` INNER JOIN `users` ON users.user_id = friends.user_id WHERE friends.friend = '".$_SESSION['user_id']."'
OR friends.user_id = '".$_SESSION['user_id']."'");

$stmt->execute();
$data = $stmt->fetchAll();

foreach ($data as $row) {

if ($row['friend'] == $_SESSION['user_id']) {

echo "<a href='profile?id=". $row['user_id'] ."'>".$row['username']."</a>";

}elseif ($row['user_id'] == $_SESSION['user_id']) {

echo "<a href='profile?id=". $row['friend'] ."'>".$row['friend']."</a>";
}
}
Posted
Comments
Richard Deeming 9-Oct-20 3:24am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
Elyas Omer 9-Oct-20 4:51am    
did you mean i have to do like this WHERE friends.friend = ?
and i use varibles instead of ? in execute function
Richard Deeming 9-Oct-20 4:52am    
You need to use parameters. If you concatenate data values into the query, then your database can be hacked.
Richard Deeming 9-Oct-20 3:25am    
Your code is also almost certainly vulnerable to cross-site scripting (XSS):
Cross Site Scripting (XSS) | OWASP[^]

You need to ensure that any user-controlled data is properly encoded when you display it.
Richard Deeming 9-Oct-20 3:26am    
As to the name being shown as a number, that's down to your data. We can't help you with that, since we don't have access to your database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900