PM System Problem [SOLVED]

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

PM System Problem [SOLVED]

Post by 62896dude »

Hello,

So I have a problem with viewing messages in my inbox. If I have more than one message, for example, here are the messages listed by ID order:

Testing (this message's id will be 5)

Blahblah (this message's id will be 3)

If I click on Blahblah, it shows up fine. If I click on Testing, it shows the Blahblah message.

Now, I know exactly what is wrong, the script is just pulling the first value that has a message with my name as to_user, but I need it to pull by if the message was directed to me AND by its proper ID so that it shows the right message.

Here is my read_message.php (I believe the problem is here):

Code: Select all

<html>
<head>
<title>Read a Message</title>
</head>
<body>
<?php
include_once 'connect.php';
 session_start();
   include_once 'logo.php';
   ?>
     <link href="style.css" rel="stylesheet" type="text/css" />
<?php
if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='home.php'>Login</a>";
  exit;
}
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("Could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
include_once 'statpanel.php';
include_once 'logo.php';
?>
<?php

$updateplayer="update messages set message_read='1' where to_user='$player'";
  mysql_query($updateplayer) or die("Could not update player");

$message = mysql_query("SELECT * FROM messages WHERE to_user = '$player'");
$message=mysql_fetch_assoc($message);

echo "<h1>Title: ".$message['message_title']."</h1><br><br>";
echo "<h3>From: ".$message['from_user']."<br><br></h3>";
echo "<h3>Message: <br>".$message['message_contents']."<br></h3>";

echo "<a href='owlery.php'>Go back to the Owlery</a>";
?>
</body>
</html>
Here is my Inbox.php (I don't think there is a problem here, but if it is anywhere else than read_message.php, it is here):

Code: Select all

<html>
<head>
<title>Your Messages</title>
</head>
<body>
<?php
include_once 'connect.php';
 session_start();
   include_once 'logo.php';
   ?>
     <link href="style.css" rel="stylesheet" type="text/css" />
<?php
if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='home.php'>Login</a>";
  exit;
}
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("Could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
include_once 'statpanel.php';
include_once 'logo.php';
?>
 <?php
// get the messages from the table.
$get_messages = mysql_query("SELECT message_id FROM messages WHERE to_user='$player' ORDER BY message_id DESC") or die(mysql_error());
$get_messages2 = mysql_query("SELECT * FROM messages WHERE to_user='$player' ORDER BY message_id DESC") or die(mysql_error());
$num_messages = mysql_num_rows($get_messages);
// display each message title, with a link to their content
echo '<ul>';
for($count = 1; $count <= $num_messages; $count++)
{

    $row = mysql_fetch_array($get_messages2);
    //if the message is not read, show "(new)" after the title, else, just show the title.
if($row['message_read'] == 0)
{
    echo '<a href="read_message.php?messageid=' . $row['message_id'] . '"><font color="#FFE920">' . $row['message_title'] . '</font></a><br>';
}else{
echo '<a href="read_message.php?messageid=' . $row['message_id'] . '">' . $row['message_title'] . '</a><br><br>';
}}
echo '</ul>';
echo '<a href="owlery.php">Go back to the Owlery</a><br><br>';

echo '<a href="index.php">Go back to the Great Hall</a>';
?>
</body>
</html>
So again, all I really need to know is how to pull a message from the DB only if the id is the id of the actual message?

I hope you guys know what I'm talking about, I found it kind of difficult to explain :P

Thanks everyone!
Last edited by 62896dude on Mon Aug 01, 2011 3:33 am, edited 1 time in total.
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: PM System Problem

Post by 62896dude »

I would also like to add: This PM System was not in any way based off of the new tutorial that halls did for PM Systems
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: PM System Problem

Post by Xaleph »

If you know the ID it`s simple:

SELECT * FROM messages WHERE message_id = $id
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: PM System Problem

Post by 62896dude »

but isn't that how you find $id?
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: PM System Problem

Post by Xaleph »

Uhm no?

Lol you want to find the ID? You already have it! Haha you have $id! That`s the ID right? I mean, what did you expect? SQL is not strict, it does what you tell it to do.

So,
SELECT * FROM messages WHERE message_id = $id
is like this:
Find me everything from the table messages where the message id is equal to $id.

Which at all times should only return 1 message, because if you have a good database design, every message has a unique ID, if not, delete that table inmediatly .
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: PM System Problem

Post by Ark »

62896dude wrote:
Now, I know exactly what is wrong, the script is just pulling the first value that has a message with my name as to_user, but I need it to pull by if the message was directed to me AND by its proper ID so that it shows the right message.
say what you need in code.

$message = mysql_query("SELECT * FROM messages WHERE to_user = '$player'");
$message=mysql_fetch_assoc($message);


$messagedestiny = $player;

if ($messagedestiny == $player && $messageid == $id)
{ echo $message; }

IDK just an idea :D
Orgullo Catracho
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: PM System Problem

Post by 62896dude »

Xaleph - Well there was never a $id defined, but I have adjusted the code a bit to add $id in there, and still nothing. Here is read_message.php now:

Code: Select all

<html>
<head>
<title>Read a Message</title>
</head>
<body>
<?php
include_once 'connect.php';
 session_start();
   include_once 'logo.php';
   ?>
     <link href="style.css" rel="stylesheet" type="text/css" />
<?php
if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='home.php'>Login</a>";
  exit;
}
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("Could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
include_once 'statpanel.php';
?>
<?php

$updateplayer="update messages set message_read='1' where to_user='$player'";
  mysql_query($updateplayer) or die("Could not update player");
  
$messageinfo="SELECT message_id from messages where to_user='$player'";
$messageinfo2=mysql_query($messageinfo) or die("Could not get player stats!");
$messageinfo3=mysql_fetch_array($messageinfo2);
$id=$messageinfo3['message_id'];

$message = mysql_query("SELECT * FROM messages WHERE message_id = '$id'");
$message1=mysql_fetch_assoc($message);

echo "<h1>Title: ".$message1['message_title']."</h1><br><br>";
echo "<h3>From: ".$message1['from_user']."<br><br></h3>";
echo "<h3>Message: <br>".$message1['message_contents']."<br></h3>";

echo "<a href='owlery.php'>Go back to the Owlery</a>";
?>
</body>
</html>
Also, something to note, in Inbox.php, it says:

Code: Select all

echo '<a href="read_message.php?messageid=' . $row['message_id'] . '"><font color="#FFE920">' . $row['message_title'] . '</font></a><br>';
That is accurate, so when clicking on Testing (ID 5) it will show in the url:

http://www.hpdestiny.net23.net/read_mes ... essageid=5

And if you click on Blahblah (ID 3) it also shows up how it should:

http://www.hpdestiny.net23.net/read_mes ... essageid=3

however, despite what the address says, it still isn't showing the proper message, it is always showing the Blahblah message.

Ark - thanks, i actually thought that was gonna work too, but same result :/
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: PM System Problem

Post by Xaleph »

remove quotes from the query, maybe that helps.
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: PM System Problem

Post by 62896dude »

Actually thanks for pointing that out Xaleph, I'm not sure why I did it like that, but that still didn't fix it :(
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: PM System Problem

Post by Xaleph »

I know, just wanted to point that out. Ok now look at this:

$messageinfo="SELECT message_id from messages where to_user='$player'"; // You are going to get a MESSAGE ID
$messageinfo2=mysql_query($messageinfo) or die("Could not get player stats!"); // you query it, it will succeed obviously..
$messageinfo3=mysql_fetch_array($messageinfo2); // Make a nice little array of it
$id=$messageinfo3['message_id']; // store the MESSAGE_ID from the query in $id.. seems odd. Why is this odd?

What is wrong here?

Think about the logic error you are making here.

edit: added notes
Post Reply

Return to “Advanced Help and Support”