Page 1 of 1

private message script not working

Posted: Thu Mar 08, 2012 4:42 pm
by dyr
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

Code: Select all

<?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 '<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'] .
'">' . $row['message_title'] .
'</a>(New)<br>';

}else{

echo '<a href="read_message.php?messageid=' . $row['message_id'] .
'">' . $row['message_title'] . '</a><br>';

}}

echo '</ul>';

echo '<form name="newmsgfrm" method="post"
action="new_message.php">';

 echo '<input type="submit" value="Send a New Message">';

 echo '</form>';

echo '<form name="backfrm" method="post" action="index.php">';

 echo '<input type="submit" value="Back to Home">';

 echo '</form>';

?>
new_message.php

Code: Select all

<?php

include('config.php');
include('date.php');

$userfinal = $_SESSION['id'];

$user = $userfinal;

?>

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

method="post">

Title: <input type="text" name="message_title"> <br />

To: <input type="text" name="message_to"><br />

Message: <br />

<textarea rows="20" cols="50" name="message_content">

</textarea>

<?php

echo '<input type="text" name="message_from"
value="'.$user.'"><br>';

?>

<input type="submit" value="Submit">

</form>
messageck.php

Code: Select all

<?php

include('config.php');
include('date.php');

$title = $_POST['message_title'];

$to = $_POST['message_to'];

$content = $_POST['message_content'];

$from = $_POST['message_from'];

$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.<br />

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

method=\"post\">

<input type=\"submit\" value=\"Try Again\">

</form>

");

}

elseif(strlen($content) < 1){

die("You can't send an empty message!<br>

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

method=\"post\">

<input type=\"submit\" value=\"Try Again\">

</form>

");

}

elseif(strlen($title) < 1){

die("You must have a Title!<br>

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

method=\"post\">

<input type=\"submit\" value=\"Try Again\">

</form>

");

}else{

mysql_query("INSERT INTO messages (from_user, to_user, message_title, message_contents) VALUES ('$from','$to','$title','$content')") or die("Error: ".mysql_error());

echo "The Message Was Successfully Sent!";

?>

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

method="post">

<input type="submit" value="Back to The Inbox">

</form>

<?php

}

?>
read_message.php

Code: Select all

<?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 "<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 '<form name="backfrm" method="post" action="inbox.php">';

 echo '<input type="submit" value="Back to Inbox">';

 echo '</form>';

?>
Any insight on why this isn't working is appreciated! The config.php page has the session start and connection to the db.

Re: private message script not working

Posted: Thu Mar 08, 2012 5:19 pm
by requinix
Can you edit your post first? Change the [php] tags to [syntax=php].

Re: private message script not working

Posted: Thu Mar 08, 2012 7:13 pm
by dyr
Oh dear, many apologies! Fixed.

Re: private message script not working

Posted: Thu Mar 08, 2012 7:16 pm
by requinix
Have you looked in the database to see if the message is actually there?

Re: private message script not working

Posted: Thu Mar 08, 2012 7:33 pm
by dyr
Sorry I didn't include this in to the original message- the messages are not showing up in the database, meaning they aren't being properly inserted/stored? But I thought I was doing everything right so I'm rather confused.

Re: private message script not working

Posted: Thu Mar 08, 2012 11:03 pm
by requinix

Code: Select all

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());
You've got a couple closing parentheses in the wrong places.

Re: private message script not working

Posted: Fri Mar 09, 2012 5:11 pm
by dyr
Thanks! Found the missing parenthesis and fixed it! 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). Also, it still says 'new' even after I click on it. Did I mess something up in the coding? Edited my original post with the new codes.

Re: private message script not working

Posted: Fri Mar 09, 2012 6:53 pm
by requinix
dyr wrote: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).
The URL is read_message.php?messageid= but your script looks for $_GET[message].
dyr wrote:Also, it still says 'new' even after I click on it.
read_message.php doesn't update that.

Re: private message script not working

Posted: Sat Mar 10, 2012 8:38 am
by dyr
Thanks again! I got it working now. My question now is, when it says 'from' it shows the User's ID#. When people send messages they use the user's ID# so that's why it's showing. I'm wondering if I could show both the display name, AND the id# in the 'from' line? ex: Dyr (#1)

What kind of editing to the codes would I need to do?

here's the updated read_message.php:

Code: Select all

<?php

include('config.php');
include('date.php');


$userfinal = $_SESSION['id'];

$messageid = $_GET['messageid'];
$message = mysql_query("SELECT * FROM messages WHERE message_id = '$messageid' AND to_user = '$userfinal'");
$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 '<form name="backfrm" method="post" action="inbox.php">';

 echo '<input type="submit" value="Back to Inbox">';

 echo '</form>';

$q="UPDATE messages SET message_read='1' WHERE message_id = '$messageid' AND to_user = '$userfinal'";
mysql_query($q);

?>

Re: private message script not working

Posted: Sat Mar 10, 2012 8:50 am
by Celauran
You could use a JOIN on the users table to get the username.

Re: private message script not working

Posted: Sun Mar 18, 2012 3:58 pm
by dyr
So something like:

Code: Select all

$userfinal = $_SESSION['id'];

$messageid = $_GET['messageid'];
mysql_query("SELECT * FROM messages, users WHERE message_id = '$messageid', id = '$messageuser' AND to_user = '$userfinal'");
$message=mysql_fetch_assoc($message);

echo "<h3>From:
".$message['from_user']."<br><br></h3>";
Am I using the join method correctly?

Re: private message script not working

Posted: Tue Apr 03, 2012 5:38 pm
by dyr
Hi, using something from the mySQL side doesn't seem to work for what I'd like to accomplish. How could I produce the same effect, but running it through the php-code side? How would I be able to get the callname from the current session? (since I use the session ID# set as the 'from' user)