PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Dec 18, 2014 1:20 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Thu Mar 08, 2012 5:42 pm 
Offline
Forum Newbie

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

Top
 Profile  
 
PostPosted: Thu Mar 08, 2012 6:19 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 5700
Location: WA, USA
Can you edit your post first? Change the [php] tags to [syntax=php].


Top
 Profile  
 
PostPosted: Thu Mar 08, 2012 8:13 pm 
Offline
Forum Newbie

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


Top
 Profile  
 
PostPosted: Thu Mar 08, 2012 8:16 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 5700
Location: WA, USA
Have you looked in the database to see if the message is actually there?


Top
 Profile  
 
PostPosted: Thu Mar 08, 2012 8:33 pm 
Offline
Forum Newbie

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


Top
 Profile  
 
PostPosted: Fri Mar 09, 2012 12:03 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 5700
Location: WA, USA
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.


Top
 Profile  
 
PostPosted: Fri Mar 09, 2012 6:11 pm 
Offline
Forum Newbie

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


Top
 Profile  
 
PostPosted: Fri Mar 09, 2012 7:53 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 5700
Location: WA, USA
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.


Top
 Profile  
 
PostPosted: Sat Mar 10, 2012 9:38 am 
Offline
Forum Newbie

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

?>


Top
 Profile  
 
PostPosted: Sat Mar 10, 2012 9:50 am 
Offline
Moderator
User avatar

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

_________________
Stay on top of upgrades.
Supported PHP versions
No longer supported versions


Top
 Profile  
 
PostPosted: Sun Mar 18, 2012 3:58 pm 
Offline
Forum Newbie

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


Top
 Profile  
 
PostPosted: Tue Apr 03, 2012 5:38 pm 
Offline
Forum Newbie

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], gautamz07 and 16 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group