kate tango kate tango - 5 months ago 14
PHP Question

displaying mysql data based on the username

i am new in php therefore i cannot figure out how to solve this issue.

<?php

// starts a session and checks if the user is logged in

error_reporting(E_ALL & ~E_NOTICE);
session_start();

if (isset($_SESSION['id'])) {
$userId = $_SESSION['id'];
$username = $_SESSION['username'];

} else {
header('Location: index.php');
die();
}


?>


i have a php login system which uses username and password from mysql database table called members. it uses session to see if the user is logged in.

<?php

include 'booking_connection.php';


$sql = "select id, name, room, computer_id, date, start_time, end_time from booked";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0 ){

while($row = mysqli_fetch_assoc($result)){


?>



<tr>
<td><?=$row['id']?></td>
<td><?=$row['name']?></td>
<td><?=$row['room']?></td>
<td><?=$row['computer_id']?></td>
<td><?=$row['date']?></td>
<td><?=$row['start_time']?></td>
<td><?=$row['end_time']?></td>
<td>
<a href="delete.php?id=<?=$row['id']?>">CANCEL</a>
</td>
</tr>

<?php

}

}


?>


i have another table called booked which stores information like name, id, date, computer no, start date and end date. i have a php code that displays all this information inside a table on the webpage. how would i make sure the system only displays information of a specific user based on their name.

i want to link the value of the session "username" with the value of the row "name" in the table.
for example: if the username saved on the session is "john", i would like the system to display all the information with the name john only.

below is the full source code for that page:

<?php

// starts a session and checks if the user is logged in

error_reporting(E_ALL & ~E_NOTICE);
session_start();

if (isset($_SESSION['id'])) {
$userId = $_SESSION['id'];
$username = $_SESSION['username'];

} else {
header('Location: index.php');
die();
}


?>



<!DOCTYPE html>

<html lang="en">


<head>

<!-- title -->
<title> Mycomputer </title>

<!-- css link -->
<link rel="stylesheet" type="text/css" href="booking.css">


</head>

<body>

<!-- headings -->

<h2>You have the following bookings.</h2>



<!-- table -->

<table>

<tr>
<th>ID No</th>
<th>Name</th>
<th>Room</th>
<th>Computer ID</th>
<th>Date</th>
<th>Start Time</th>
<th>End Time</th>
<th>Action</th>
</tr>

<?php

include 'booking_connection.php';


$sql = "select id, name, room, computer_id, date, start_time, end_time from booked";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0 ){

while($row = mysqli_fetch_assoc($result)){


?>



<tr>
<td><?=$row['id']?></td>
<td><?=$row['name']?></td>
<td><?=$row['room']?></td>
<td><?=$row['computer_id']?></td>
<td><?=$row['date']?></td>
<td><?=$row['start_time']?></td>
<td><?=$row['end_time']?></td>
<td>
<a href="delete.php?id=<?=$row['id']?>">CANCEL</a>
</td>
</tr>

<?php

}

}


?>

</table>

</body>

</html>

Answer

In the sql use a WHERE clause to get only the filtered rows:

$sql = "
    SELECT 
    id, name, room, computer_id, date, start_time, end_time 
    FROM
    booked 
    WHERE 
    name = '" . $_SESSION['username'] ."'"
;
$result = mysqli_query($con, $sql);