Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, so I tried to create a profile page where I sort data from my table based on the user connect. This don't work as I intended it to be, I get no results after the query is completed.

What I have tried:

My mesajeuseri table contain 4 fields: id, username, message, expeditor.
My user table contain 5 fields: id, name, username, email, password.
So as you can see below I tried to sort my data from mesajeuseri based on the user which is connected into the website, but there is no record on the page, and I am logged with the specific user which have some records in the table.

<?php

require 'config.php';
	if(!empty($_SESSION["id"]))
	{
		$id= $_SESSION["id"];
		$result = mysqli_query($conn,"SELECT * FROM tb_user WHERE id= $id");
		$row = mysqli_fetch_assoc($result);
	}
	else
	{
		header("Location: login.php");
		
	}

?>


								



<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Profile</title>
	<style type="text/css">
		input{
			border: none;
		}
	</style>
</head>
<body>

<table  cellspacing= 0 cellpadding=10>
		<tr>
			<td>#</td>
			<td>Expeditor</td>
			<td>Mesaj</td>								
		</tr>
		<?php
			$i=1;
			$id2= $_SESSION["id"];
			$rows= mysqli_query($conn, "SELECT * FROM mesajeuseri WHERE username=(SELECT username FROM tb_user WHERE id = '$id2')");
			
									
		?>
		<?php foreach ($rows as $row): ?>
		<tr>
			<td><?php echo $i++; ?></td>
			<td><?php echo $row["expeditor"] ?></td>
			<td><?php echo $row["descriere"]; ?></td>

			<td>
				<a href="">Contact</a>
			</td>
		</tr>
		<?php endforeach; ?>	
</table>
				




</body>
</html>
Posted
Updated 27-May-22 1:21am

1 solution

Ok, I figure it out. The problem was about my table, there was a space character before my actual text, this make the condition useless. Here is the correct code maybe someone will find this useful. But be careful this is wide open to SQL Injection.

<?php

require 'config.php';
	if(!empty($_SESSION["id"]))
	{
		$id= $_SESSION["id"];
		$result = mysqli_query($conn,"SELECT * FROM tb_user WHERE id= $id");
		$row = mysqli_fetch_assoc($result);
	}
	else
	{
		header("Location: login.php");
		
	}

?>						

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Profile</title>
	<style type="text/css">
		input{
			border: none;
		}
	</style>
</head>
<body>

<table  cellspacing= 0 cellpadding=10>
		<tr>
			<td>#</td>
			<td>Expeditor</td>
			<td>Mesaj</td>								
		</tr>
		<?php
			$i=1;
			$id2= $_SESSION["id"];
			$result= mysqli_query($conn, "SELECT * FROM tb_user WHERE id= $id2");
			$row = $result->fetch_assoc();
			$userconn = $row['username'];
			echo $userconn;
			//$rows= mysqli_query($conn, "SELECT * FROM mesajeuseri WHERE username=(SELECT username FROM tb_user WHERE id = '$id2')");
			$rows= mysqli_query($conn, "SELECT * FROM mesajeuseri WHERE username='$userconn'")
									
		?>
		<?php foreach ($rows as $row): ?>
		<tr>
			<td><?php echo $i++; ?></td>
			<td><?php echo $row["expeditor"] ?></td>
			<td><?php echo $row["mesajtext"]; ?></td>

			<td>
				<a href="">Contact</a>
			</td>
		</tr>
		<?php endforeach; ?>	
</table>
		
</body>
</html>
 
Share this answer
 
Comments
CHill60 30-May-22 2:50am    
One small piece of advice - avoid using SELECT *. It is better to list the actual columns you need - that way if the table schema changes (add/remove columns) your code does not break

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