private message script not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

private message script not working

Postby dyr » Thu Mar 08, 2012 5:42 pm

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
Syntax: [ Download ] [ Hide ]
<?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
Syntax: [ Download ] [ Hide ]
<?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
Syntax: [ Download ] [ Hide ]
<?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
Syntax: [ Download ] [ Hide ]
<?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.
Last edited by dyr on Fri Mar 09, 2012 6:13 pm, edited 2 times in total.
dyr
Forum Newbie
 
Posts: 22
Joined: Fri Mar 02, 2012 3:34 pm

Re: private message script not working

Postby requinix » Thu Mar 08, 2012 6:19 pm

Can you edit your post first? Change the [php] tags to [syntax=php].
Did you steal it?
User avatar
requinix
Spammer :|
 
Posts: 5479
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: private message script not working

Postby dyr » Thu Mar 08, 2012 8:13 pm

Oh dear, many apologies! Fixed.
dyr
Forum Newbie
 
Posts: 22
Joined: Fri Mar 02, 2012 3:34 pm

Re: private message script not working

Postby requinix » Thu Mar 08, 2012 8:16 pm

Have you looked in the database to see if the message is actually there?
Did you steal it?
User avatar
requinix
Spammer :|
 
Posts: 5479
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: private message script not working

Postby dyr » Thu Mar 08, 2012 8:33 pm

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.
dyr
Forum Newbie
 
Posts: 22
Joined: Fri Mar 02, 2012 3:34 pm

Re: private message script not working

Postby requinix » Fri Mar 09, 2012 12:03 am

Syntax: [ Download ] [ Hide ]
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.
Did you steal it?
User avatar
requinix
Spammer :|
 
Posts: 5479
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: private message script not working

Postby dyr » Fri Mar 09, 2012 6:11 pm

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.
dyr
Forum Newbie
 
Posts: 22
Joined: Fri Mar 02, 2012 3:34 pm

Re: private message script not working

Postby requinix » Fri Mar 09, 2012 7:53 pm

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.
Did you steal it?
User avatar
requinix
Spammer :|
 
Posts: 5479
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: private message script not working

Postby dyr » Sat Mar 10, 2012 9:38 am

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:
Syntax: [ Download ] [ Hide ]
<?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);

?>
dyr
Forum Newbie
 
Posts: 22
Joined: Fri Mar 02, 2012 3:34 pm

Re: private message script not working

Postby Celauran » Sat Mar 10, 2012 9:50 am

You could use a JOIN on the users table to get the username.
User avatar
Celauran
Moderator
 
Posts: 3116
Joined: Tue Nov 09, 2010 3:39 pm
Location: Montreal, Canada

Re: private message script not working

Postby dyr » Sun Mar 18, 2012 3:58 pm

So something like:

Syntax: [ Download ] [ Hide ]
$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?
dyr
Forum Newbie
 
Posts: 22
Joined: Fri Mar 02, 2012 3:34 pm

Re: private message script not working

Postby dyr » Tue Apr 03, 2012 5:38 pm

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)
dyr
Forum Newbie
 
Posts: 22
Joined: Fri Mar 02, 2012 3:34 pm


Return to PHP - Code

Who is online

Users browsing this forum: Majestic-12 [Bot], Yahoo [Bot] and 12 guests