I'm currently working on a PM system (private message system). However, I am getting a error.
The error is:
Notice: Trying to get property of non-object in C:\xampp\htdocs\brickmarket\messages.php on line 18
I have tested this without the WHERE to='" . $_SESSION["user_name"] . "'";
and that works fine and the error doesn't appear, so I am unsure what the issue is.
$_SESSION["user_name"]
outputs the username of the currently logged in user.
My full code is:
<h1>My private messages</h1>
<?php
$sql = "SELECT * FROM pb WHERE to='" . $_SESSION["user_name"] . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table width="100%">
<tr>
<td><strong>ID</strong></td><td><strong>From</strong></td><td><strong>To</strong></td><td><strong>Subject</strong></td><td><strong>Date</strong></td><td></td>
</tr>
<?php
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row["id"]; ?></td><td><?php echo $row["from"]; ?></td><td><?php echo $row["to"]; ?></td><td><?php echo $row["subject"]; ?></td><td><?php echo $row["date"]; ?></td><td><a href="">View</a></td>
</tr>
<?php
}
?>
</table>
<?php
} else {
echo "You don't have any messages yet.";
}
?>
I hope someone can help me out, I'm fighting with my code for a few hours now.
PS: I know this code is vulnerable to SQL injection & will fix that later.