Private Messaging system

DealthRune 1 Tallied Votes 255 Views Share

This one is a messaging system!

Made it myself, works great!

Hopes you like :)

<?php
//name this page inbox.php
?>






<?php
// get the messages from the table.
$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
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><br>';
}else{
echo '<a href="read_message.php?messageid=' . $row['message_id'] . '">' . $row['message_title'] . '</a><br>';
}}
echo '<form name="newmsgfrm" method="post" action="new_message.php">';
echo '<br><input type="submit" value="Send a New Message"><br>';
echo '</form>';

echo '<form name="backfrm" method="post" action="members.php">';
echo '<input type="submit" value="Back to Home"><br>';
echo '</form>';
?>







<?php
//Name this page messageck.php
//(yes like that)
?>








<?php

$title=$_POST['message_title'];
$to=$_POST['message_to'];
$content=$_POST['message_content'];
$from=$_POST['message_from'];


$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 don't excist. 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("Your 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("Could not send the message: <br>".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
}
?>








<?php
//Name this one new_message.php
?>







<form name="message" action="messageck.php"
method="post">
Title: <input type="text" name="message_title" value="<?php if($use){echo 'This Is A Message'; }?>"><br>
To: <input type="text" name="message_to" value="<?php if($use){echo $use; }?>"><br>
Message: <br>
<textarea rows="20" cols="50" name="message_content">
</textarea>
<?php
echo '<input type="hidden" name="message_from" value="'.$user.'"><br>';
?>
<input type="submit" value="Send">
</form>










<?php
//Name this page read_message.php
?>









<?php

$messageid = $_GET['messageid'];
$message = mysql_query("SELECT * FROM messages WHERE message_id = '$messageid' AND to_user = '$userfinal'");
$message=mysql_fetch_assoc($message);
mysql_query("UPDATE messages SET message_read = '1' WHERE message_id = '$messageid' AND to_user = '$userfinal'");

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

echo '<form name="backfrm" method="post" action="inbox.php">';
echo '<input type="submit" value="Back to Inbox">';
echo '</form>';
?>








<?php
//And finally import this sql into your database
?>








CREATE TABLE IF NOT EXISTS `messages` (
  `message_id` int(11) NOT NULL AUTO_INCREMENT,
  `from_user` varchar(65) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `to_user` varchar(65) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `message_title` varchar(65) NOT NULL,
  `message_contents` longtext NOT NULL,
  `message_read` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`message_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=32 ;
somedude3488 228 Nearly a Posting Virtuoso

Where is $userfinal coming from. It's not defined anywhere.

rohm4n 0 Newbie Poster

From here maybe...:
<?php

$messageid = $_GET;
$message = mysql_query("SELECT * FROM messages WHERE message_id = '$messageid' AND to_user = '$userfinal'");
$message=mysql_fetch_assoc($message);
mysql_query("UPDATE messages SET message_read = '1' WHERE message_id = '$messageid' AND to_user = '$userfinal'");

echo "<p>Title: ".$message."</p>";
echo "<p>From: ".$message."</p>";
echo "<p>Message: <br>".$message."<br></p>";

echo '<form name="backfrm" method="post" action="inbox.php">';
echo '<input type="submit" value="Back to Inbox">';
echo '</form>';
?>

xueping 0 Newbie Poster

I've copy and paste into my database but it's not working.
an error prompt

# 
Warning: mysql_query() [function.mysql-query]: Access denied for user 'apache'@'localhost' (using password: NO) in /home/mp02/public_html/messageck.php on line 24

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/mp02/public_html/messageck.php on line 24

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mp02/public_html/messageck.php on line 24
The user you are trying to contact don't excist. Please go back and try again.
#

any solution to solve this? there's nothing on my line 24.

smartness -3 Junior Poster

@xueping:

You have to set a mysql connection to you DB... example:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

then select your DB:
mysql_select_db($db_database,$link);

LloydFarrell 0 Junior Poster

Hi, many thanks for sharing this -

I am trying to apply your code, but i have a few errors ?

can you help me out please !

from the inbox.php when I open the page I get
No Database Selected ??

when I nav to messageck.php i get the following error message

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/m/y/v/myvipdeals/html/PHP-Login/PHP-Login/messageck.php on line 131

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/m/y/v/myvipdeals/html/PHP-Login/PHP-Login/messageck.php on line 131

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/m/y/v/myvipdeals/html/PHP-Login/PHP-Login/messageck.php on line 131
The user you are trying to contact don't excist. Please go back and try again.

READ_MESSAGE.php also gives me an error message

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/m/y/v/myvipdeals/html/PHP-Login/PHP-Login/read_message.php on line 125

Title: 

From: 

Message:

I am new to php and would like to learn much more, If someone could reply explaining where i am going wrong I would be very much appreicated as I am keen to learn and teach myself new programming skills,

Thank you for taking your time to read my post.

Happy Holidays Everyone ;-)

smartness -3 Junior Poster

@LloydFarrell:

I think the second error is caused from the first error!

LloydFarrell 0 Junior Poster

@smartness:

Cheers, but how do i fix the first error ?

<?php
$link = mysql_connect('host','username','password','database');
mysql_select_db($db_database,$link);
$title=$_POST['message_title'];
$to=$_POST['message_to'];
$content=$_POST['message_content'];
$from=$_POST['message_from'];


$ck_reciever = "SELECT login FROM members WHERE login = '".$to."'";

"this is the line that is causing the error " 
if(mysql_num_rows( mysql_query($ck_reciever))==0){
die("The user you are trying to contact don't excist. 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("Your 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("Could not send the message: <br>".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
}
?>
smartness -3 Junior Poster

@LloydFarrell
Make sure you replace the values of host, username, password

aslo the $db_database needs to be defined
$db_database = "DATABASE NAME HERE";
also, remove the link 'database', i don't know why you should use it?!

$link = mysql_connect('localhost','your_username','your_db_password');
mysql_select_db($db_database, $link);

LloydFarrell 0 Junior Poster

@smartness

Cheers for the reply, I done some digging around here after i replied and found some code pretty much the same as you have placed on here,

Cheers.

I'v now got rid of all the error messages and can send email to the database.

the only problem im now having is viewing the messages.

has anyone else had any other issues viewing the messages from the messages table ?

Also, I dont seem to be able to upload to the "from_user" field as this is blank ??

jamied_uk 0 Newbie Poster

same here what am i doin wrong please help good tut tho i have no errors just having blank messages
i need to put a bit of code in login part so sessions and db match up as its sending but not allowin to see the messages which do exist in db with missing dates :/ help plz im a noob in need :d

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.