pm system - inbox not working

I’ve added a simple pm system on to my localhost recently and while I can send messages successfully, I can’t receive them and new messages don’t show up in the inbox.

inbox.php
[php]

<?php include('date.php'); include('config.php'); $userfinal = $_SESSION['id']; $get_messages = mysql_query("SELECT message_id FROM messages WHERE to_user='$userfinal' ORDER BY message_id DESC") or die(mysql_error()); $get_messages2 = mysql_query("SELECT * FROM messages WHERE to_user='$userfinal' 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 '
    '; 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 '' . $row['message_title'] . '(New)
    '; }else{ echo '' . $row['message_title'] . '
    '; }} echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; ?>[/php]

new_message.php
[php]<?php

include(‘date.php’);

include(‘config.php’);

$userfinal = $_SESSION[‘id’];

$user = $userfinal;

?>

<form name="message" action="messageck.php"

method=“post”>

Title:

To:

Message:

<?php echo '
'; ?>

[/php]

messageck.php
[php]<?php

include(‘date.php’);

include(‘config.php’);

$title = $_POST[‘message_title’];

$to = $_POST[‘message_to’];

$content = $_POST[‘message_content’];

$from = $_POST[‘message_from’];

$time = $_POST[‘message_date’];

$ck_reciever = “SELECT username FROM users WHERE username = '”.$to."’";

	if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 ){

die("The user you are trying to contact doesn’t exist. Please go back and try again.

<form name=\"back\" action=\"new_message.php\"

method=“post”>

<input type=“submit” value=“Try Again”>

");

}

elseif(strlen($content) < 1){

die("You can’t send an empty message!

<form name=\"back\" action=\"new_message.php\"

method=“post”>

<input type=“submit” value=“Try Again”>

");

}

elseif(strlen($title) < 1){

die("You must have a Title!

<form name=\"back\" action=\"new_message.php\"

method=“post”>

<input type=“submit” value=“Try Again”>

");

}else{

@mysql_query(“INSERT INTO messages (from_user, to_user, message_title, message_contents,
message_date) VALUES (’$from’,’$to’,’$title’,’$content’,’$time’)” OR die(‘Could not send the message’).mysql_error());

echo “The Message Was Successfully Sent!”;

?>

<form name="back" action="inbox.php"

method=“post”>

<?php } ?>[/php]

read_message.php
[php]<?php

include(‘date.php’);

include(‘config.php’);

$userfinal = $_SESSION[‘id’];

$messageid = $_GET[‘message’];

$message = mysql_query(“SELECT * FROM messages WHERE message_id = ‘$messageid’ AND
to_user = ‘$userfinal’”);

$message = mysql_fetch_assoc($message);

echo “

Title:
“.$message[‘message_title’].”



”;

echo “

From:
“.$message[‘from_user’].”

”;

echo “

Message:

“.$message[‘message_contents’].”

”;

echo ‘’;

echo ‘’;

echo ‘’;

?>[/php]

Any insight on to why this isn’t working would be appreciated!

why are you starting the count at 1 in that first bit of code? i would start by verifying that the information is a) getting passed between the pages and b) getting entered into the database. If b is correct, then its just an issue with it being retrieved.

So should I go with an if-else statement instead of setting the count? And I thought my messageck.php was verifying the information? Should I redo that segment of code? Sorry for all the questions! Just a bit confused.

There’s no POST data for the date coming from that new message form, that’s 1 big problem that i see since its being entered into the db table. Also, when you’re doing development work, don’t surpress error messages. If there is an issue, you’ll never know what it is. Once it’s fixed, then you can surpress it.

How are the messages being stores in the table? by user name or user id? you always want something unique, there’s a thousand different ways to spell john smith and mysql is very specific on its search rules.

Okay, error messages should be unsurpressed. I removed all the @'s.

For messages being stored, I’m using message_id. For the to_user section, users can enter the other player’s ID# to send a message to them.

Will include the date in a second and give you an update.

Added the message date so it’ll show up in the inbox. Am I adding this correctly? Never did it where more than 2 variables were required here.

[php]echo ‘’ . $row[‘message_title’] . . $row[‘message_date’] . ‘
’;

}}
[/php]

Remove the extra double quotes and the " . . " only one needed…

Thanks! Still doesn’t seem to be working, do you guys see any other errors?

Can you repost it with the new changes in place… Maybe we missed something…

Sure thing! Didn’t make many changes besides unsuppressing some error messages and adding the $date:

inbox.php

[code]<?php

include(‘config.php’);
include(‘date.php’);

$userfinal = $_SESSION[‘id’];

$get_messages = mysql_query(“SELECT message_id FROM messages WHERE
to_user=’$userfinal’ ORDER BY message_id DESC”) or die(mysql_error());

$get_messages2 = mysql_query(“SELECT * FROM messages WHERE to_user=’$userfinal’
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 ‘

    ’;

    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'] .

‘">’ . $row[‘message_title’] .
‘(New)
’;

}else{

echo ‘’ . $row[‘message_title’] . $row[‘message_date’] . ‘
’;

}}

echo ‘’;

echo ‘’;

echo ‘’;

echo ‘’;

echo ‘’;

echo ‘’;

echo ‘’;

?>[/code]

new_message.php

[code]<?php

include(‘config.php’);
include(‘date.php’);

$userfinal = $_SESSION[‘id’];

$user = $userfinal;

?>

<form name="message" action="messageck.php"

method=“post”>

Title:

To:

Message:

<?php echo '
'; ?> [/code]

messageck.php

[code]<?php

include(‘config.php’);
include(‘date.php’);

$title = $_POST[‘message_title’];

$to = $_POST[‘message_to’];

$content = $_POST[‘message_content’];

$from = $_POST[‘message_from’];

$time = $_POST[‘message_date’];

$ck_reciever = “SELECT id FROM users WHERE id = '”.$to."’";

	if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 ){

die("The user you are trying to contact doesn’t exist. Please go back and try again.

<form name=\"back\" action=\"new_message.php\"

method=“post”>

<input type=“submit” value=“Try Again”>

");

}

elseif(strlen($content) < 1){

die("You can’t send an empty message!

<form name=\"back\" action=\"new_message.php\"

method=“post”>

<input type=“submit” value=“Try Again”>

");

}

elseif(strlen($title) < 1){

die("You must have a Title!

<form name=\"back\" action=\"new_message.php\"

method=“post”>

<input type=“submit” value=“Try Again”>

");

}else{

mysql_query(“INSERT INTO messages (from_user, to_user, message_title, message_contents,
message_date) VALUES (’$from’,’$to’,’$title’,’$content’,’$time’)” OR die(‘Could not send the message’).mysql_error());

echo “The Message Was Successfully Sent!”;

?>

<form name="back" action="inbox.php"

method=“post”>

<?php } ?>[/code]

read_message.php

[code]<?php

include(‘config.php’);
include(‘date.php’);

$userfinal = $_SESSION[‘id’];

$messageid = $_GET[‘message’];

$message = mysql_query(“SELECT * FROM messages WHERE message_id = ‘$messageid’ AND
to_user = ‘$userfinal’”);

$message = mysql_fetch_assoc($message);

echo “

Title:
“.$message[‘message_title’].”



”;

echo “

From:
“.$message[‘from_user’].”

”;

echo “

Message:

“.$message[‘message_contents’].”

”;

echo ‘’;

echo ‘’;

echo ‘’;

?>[/code]

It’s not showing up in the database either, so it’s not being properly stored for some reason.

Had some parenthesis in the wrong places, fixed them and now it’s storing it in the database! However now when i click on the message, it shows up blank (even though the message is being stored in the databse, it’s just not being properly showed). And the ‘new’ (for new message) is still there even after I click on it.

I believe the message shows up blank because the url is read_message.php?messageid= while I’m only $_GET[‘message’] If that’s the case, changing the ‘message’ to ‘messageid’ would work, right?

Also not sure how to update read_message.php so the new goes away once the user reads it (clicks the link).

Ok, so now I got this working. I had to add an update of checking if it’s new or not, changing the ‘message’ to ‘messageid’, etc. So now it works like I’ve programmed! My question is, when a user gets a new message, is there any way to program an alert they receive? (that won’t go away until clicked) Or, something like what phphelp has, where the Inbox was a (# of messages) by it when new messages arrive.

Bump

Sorry, bumping doesn’t work if nobody is home… LOL Also, Sunday here…

Anyway, it is working as you programmed it. That is a good thing. To add an alert to wake a user up to see that they have a message, you can do that several ways. I like to have a small, but, colored notice, like “New Message Waiting!” somewhere on the page just after logging in. They log in and the very first page they encounter after that would show an alert. (Small, but, different color so it jumps out to them.)

So, is that what you were asking? If so, after logging in, the page displayed would check for a new message with a query and if the results of the query (counting new messages) is larger than 0 it would display a link that would steer them to look at the message. Not sure if that is what you were asking.

In the code you posted before, you do a search for new messages. You would just do that and count the rows of data found. If >0, then display the notice… Should be simple enough to do. Tomorrow, I plan to be on this site a lot, so ask your questions… good luck…

Would you prefer I solve this topic and make a new one more relevant to what I’d like to modify?

Here’s what I have so far:
[php]$userfinal = $_SESSION[‘id’];

$get_messages = mysql_query(“SELECT message_id FROM messages WHERE
to_user=’$userfinal’ ORDER BY message_id DESC”) or die(mysql_error());

$get_messages2 = mysql_query(“SELECT * FROM messages WHERE to_user=’$userfinal’
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 ‘’;

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 ‘You have a new message!

’;

}else{

echo ‘’;

}}
[/php]

And it works! However it seems a bit redundant, mysql_querying message_id’s, descriptions, rows, etc. Would you mind shedding some light on how I could edit this a bit and just cut it down to what I need (check if new message is in inbox), or suggesting a new way of implementing this? Now that I have this system working I’m super hesitant to do much messing around with it.

definitely a little redundancy in there it should be more like:
[php]
$userfinal = $_SESSION[‘id’];
$get_messages = mysql_query(“SELECT * FROM messages WHERE
to_user=’$userfinal’ 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 ‘’;
for($count = 1; $count <= $num_messages; $count++);
{

$row = mysql_fetch_assoc($get_messages);

//if the message is not read, show “(new)” after the title, else, just show the title.
if($row[‘message_read’] == 0)
{
echo ‘You have a new message!

’;
}else{
echo ‘’;
}}
[/php]

Thanks so much plintu! It works fine now!

Time to work on making a ‘replies’ section. I’ll go research a bit and see what method works best with my current message system. Thanks for your help, guys.

Hey, Dyr, glad Plintu could help you out. I had to deal with some sad stuff and was not home…

Always nice when a puzzle is solved. Glad you found the help you needed… Good luck with the rest…

Sponsor our Newsletter | Privacy Policy | Terms of Service