Display only the current user's details using PHP and MySQL

In this part of the code, both the score table and the profile page display everything I want, however, it is displaying EVERYBODIES details like
First Name Surname Email Category Username, However, I want it so that when the user is logged in they can only see THEIR OWN details

This is score.php

  <?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
require("db_connect.php");
session_start();
?>
 
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../../../favicon.ico">
 
<?php
$con=mysqli_connect("localhost","username","Password","Database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
 
 
$result = mysqli_query($con,"SELECT * FROM Score where 'Username' LIKE _['Username']");
 
 
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Username</th>
<th>Score</th>
<th>Gamedate</th>
<th>QuizTitle</th>
</tr>";
 
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Username'] . "</td>";
echo "<td>" . $row['Score'] . "</td>";
echo "<td>" . $row['Gamedate'] . "</td>";
echo "<td>" . $row['QuizTitle'] . "</td>";
echo "</tr>";
}
echo "</table>";
 
mysqli_close($con);
?>
this is profile.php
 <?php
 require('db_connect.php');
$result = mysqli_query($conn,"SELECT *FROM Users ");
while($row = mysqli_fetch_array($result))
{
       echo "<br />Your <b><i>Profile</i></b> is as follows:<br />";
        echo "<b>First name:</b> ". $row['FirstName'];
        echo "<br /><b>Last name:</b> ".$row['Surname'];
        echo "<br /><b>Email:</b> ".$row['Email'];
        echo "<br /><b>Year:</b> ".$row['Username'];
        echo "<br /><b>Date created :</b> ".$row['Date_Creation'];
}
mysqli_close($conn);
?>
    </main>



</html>

And this is profile.php



 <?php
 require('db_connect.php');
$result = mysqli_query($conn,"SELECT *FROM Users ");
while($row = mysqli_fetch_array($result))
{
       echo "<br />Your <b><i>Profile</i></b> is as follows:<br />";
        echo "<b>First name:</b> ". $row['FirstName'];
        echo "<br /><b>Last name:</b> ".$row['Surname'];
        echo "<br /><b>Email:</b> ".$row['Email'];
        echo "<br /><b>Year:</b> ".$row['Username'];
        echo "<br /><b>Date created :</b> ".$row['Date_Creation'];
}
mysqli_close($conn);
?>
    </main>



</html>

What have you tried so far?

1 Like

You are missing the WHERE condition.

Yes that will select everything.

I would expect at least to see something like:-

SELECT * FROM Users WHERE id = :id 

…to narrow the selection to one specific user.

I prefer using this syntax:

$qry  = "SELECT * FROM Score where 'Username' LIKE _['Username']";

// ============ DEBUG ============
echo '<br>$qry ==><br> ' .$qry;
// try copying and pasting $qry ino PhpMyAdmin 
// and 
// see if the query is doing what you want it to do.  
// ============ DEBUG ============

$result = mysqli_query($con, $qry );

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.