Jump to content

Private Messaging System


supermerc

Recommended Posts

Hey, I found a tutorial on how to make a private messaging system, but it uses cookies, and I use sessions, and i'm pretty new to php and I dont know where my mistakes were.

 

First off all the error im getting is when U click on inbox or new message, its supposed to bring up a page to compose or to my inbox but it just shows a page not found I dont understand why, and I though it might just be because of my updates but I tried with original coding and it still didnt work, So i though maybe it doesnt work because (my modified one) because I didnt modify it right. And the original one because I dont have cookies, but I dont know :-S

 

here is my code:

 

<?php
// Your database connection
$username = "vfdh; //Username
$pwd ="fghfghf"; //User password
$host = "ghfghfgh"; //host is localhost - even for most web hosts
$dbname = "sghfghm"; //db name to be accessed
//connect to db
$conn=mysql_connect($host, $username, $pwd) or die ("Unable to connect to database");
if (!($conn=mysql_connect($host, $username, $pwd))) {
printf("We couldn't connect to the database right now!");
exit;
}
$db=mysql_select_db($dbname,$conn) or die("Unable to connect to database!");
?>
| <a href="mail.php?action=compose">Compose</a> | <a href="mail.php?action=inbox">Inbox</a> |
<table cellpadding="1" cellspacing="1" height="300" width="450">
<tr><td align=center valign=top>
<?php
$table="mail"; // Only change if your mail table is not mail
$_SESSION['s_username'] = $username; // Change this to the name of your cookie

//If the url is ?action=compose then it shows the form
if($action==compose) {
echo "<form action=mail.php?action=compose2 method=post>\n";
echo "<table>\n";
echo "<tr><td>Subject:</td><td><input type=text name=subject size=20 value=$subject></td></tr>\n";
echo "<tr><td>To:</td><td><input type=text name=to size=20 value=$to></td></tr>\n";
echo "<tr><td>Message:</td>\n";
echo " <td><textarea rows=16 cols=45 name=message>\n";
echo " </textarea>\n";
echo " </textarea>\n";
echo " <td>\n";
echo "</table>";
echo "<button type=submit>Send Mail!</button>";
echo "</form>";
}
//If the url is ?action=compose2 then it sends the form
if($action==compose2) {
$subject or die("Subject Blank");
$message or die("Message Black");
$to or die("To blank");
$date = date(YmdHis);

//This inserts the message info into the db
$create = "INSERT INTO $table (UserTo, UserFrom, Subject, Message, SentDate, status)
VALUES ('$to','$username',
'$subject','$message','$date','unread')";
$create2 = mysql_query($create) or die("A letter could not be sent to $to!");
echo("Message Sent to $to!");

}
//If the url is ?action=inbox then it shows you all the messages
if($action==inbox) {
//This gets all messages from the db
$result=mysql_query("select * from $table where UserTo='$username' ORDER BY SentDate DESC") or die ("cant do it");
echo "<table cellpadding=2 cellspacing=1 width=500 valign=top>";
while ($row=mysql_fetch_array($result)) {
echo "<tr><td width=30>Mail:</td><td><a href=mail.php?action=view&mail_id=$row[mail_id]>
$row[subject]
</a></td><td width=50> <a href=mail.php?action=delete&id=$row[mail_id]>
<center>Delete
</a><br></td></tr>";
}
echo "</table>";
}
if($action==view) {
//If user has selected to view a message then it fetches the message from the database
$result=mysql_query("select * from $table where UserTo='$_COOKIE[$cookie]' and mail_id=$mail_id") or die ("cant do it");
$row=mysql_fetch_array($result);
if($row[userTo]==$_SESSION['s_username']) {
} else {
echo "<font face=verdana><b>This isn't your mail!";
exit;
}
//If user has read message this updates db saying it has been read
$query="UPDATE $table SET status='read' WHERE UserTo='$username' AND mail_id='$row[mail_id]'";
$query or die("An error occurred resulting that this message has not been marked read.");
echo "<table border = 1 bordercolor = black width = 50% align=center><tr><td>$row[subject]</td><td>
$row[userFrom]
</td></tr><tr><td colspan='2'>$row[Message]<br><a href=mail.php?action=compose&to=
$row[userFrom]&subject=RE:$row[subject]>
Reply</a></td></tr></table>";
$rs = mysql_query("UPDATE $table SET status='read' WHERE mail_id='$mail_id'");
}
if($action==delete) {
//If told to delete, Delete message from the database
$query = mysql_query("DELETE FROM $table WHERE mail_id='$id' LIMIT 1");
if($query) {
echo "<font face=verdana>Message Deleted.</font>";
} else {
echo "The message wasnt deleted.";
}
}
?>

 

Here is original code:

 

<?php
// Your database connection
$username = "sgdfgdfg"; //Username
$pwd ="dfgdfgfdgdfgm"; //User password
$host = "fddfgdfgdfgis localhost - even for most web hosts
$dbname = "supgdfgerm"; //db name to be accessed
//connect to db
$conn=mysql_connect($host, $username, $pwd) or die ("Unable to connect to database");
if (!($conn=mysql_connect($host, $username, $pwd))) {
printf("We couldn't connect to the database right now!");
exit;
}
$db=mysql_select_db($dbname,$conn) or die("Unable to connect to database!");
?>
| <a href="mail.php?action=compose">Compose</a> | <a href="mail.php?action=inbox">Inbox</a> |
<table cellpadding="1" cellspacing="1" height="300" width="450">
<tr><td align=center valign=top>
<?php
$table="mail"; // Only change if your mail table is not mail
$cookie="php4all_user"; // Change this to the name of your cookie

//If the url is ?action=compose then it shows the form
if($action==compose) {
echo "<form action=mail.php?action=compose2 method=post>\n";
echo "<table>\n";
echo "<tr><td>Subject:</td><td><input type=text name=subject size=20 value=$subject></td></tr>\n";
echo "<tr><td>To:</td><td><input type=text name=to size=20 value=$to></td></tr>\n";
echo "<tr><td>Message:</td>\n";
echo " <td><textarea rows=16 cols=45 name=message>\n";
echo " </textarea>\n";
echo " </textarea>\n";
echo " <td>\n";
echo "</table>";
echo "<button type=submit>Send Mail!</button>";
echo "</form>";
}
//If the url is ?action=compose2 then it sends the form
if($action==compose2) {
$subject or die("Subject Blank");
$message or die("Message Black");
$to or die("To blank");
$date = date(YmdHis);

//This inserts the message info into the db
$create = "INSERT INTO $table (UserTo, UserFrom, Subject, Message, SentDate, status)
VALUES ('$to','$_COOKIE[$cookie]',
'$subject','$message','$date','unread')";
$create2 = mysql_query($create) or die("A letter could not be sent to $to!");
echo("Message Sent to $to!");

}
//If the url is ?action=inbox then it shows you all the messages
if($action==inbox) {
//This gets all messages from the db
$result=mysql_query("select * from $table where UserTo='$_COOKIE[$cookie]' ORDER BY SentDate DESC") or die ("cant do it");
echo "<table cellpadding=2 cellspacing=1 width=500 valign=top>";
while ($row=mysql_fetch_array($result)) {
echo "<tr><td width=30>Mail:</td><td><a href=mail.php?action=view&mail_id=$row[mail_id]>
$row[subject]
</a></td><td width=50> <a href=mail.php?action=delete&id=$row[mail_id]>
<center>Delete
</a><br></td></tr>";
}
echo "</table>";
}
if($action==view) {
//If user has selected to view a message then it fetches the message from the database
$result=mysql_query("select * from $table where UserTo='$_COOKIE[$cookie]' and mail_id=$mail_id") or die ("cant do it");
$row=mysql_fetch_array($result);
if($row[userTo]==$_COOKIE[$cookie]) {
} else {
echo "<font face=verdana><b>This isn't your mail!";
exit;
}
//If user has read message this updates db saying it has been read
$query="UPDATE $table SET status='read' WHERE UserTo='$_COOKIE[$cookie]' AND mail_id='$row[mail_id]'";
$query or die("An error occurred resulting that this message has not been marked read.");
echo "<table border = 1 bordercolor = black width = 50% align=center><tr><td>$row[subject]</td><td>
$row[userFrom]
</td></tr><tr><td colspan='2'>$row[Message]<br><a href=mail.php?action=compose&to=
$row[userFrom]&subject=RE:$row[subject]>
Reply</a></td></tr></table>";
$rs = mysql_query("UPDATE $table SET status='read' WHERE mail_id='$mail_id'");
}
if($action==delete) {
//If told to delete, Delete message from the database
$query = mysql_query("DELETE FROM $table WHERE mail_id='$id' LIMIT 1");
if($query) {
echo "<font face=verdana>Message Deleted.</font>";
} else {
echo "The message wasnt deleted.";
}
}
?>

 

Thanks a lot!

Link to comment
https://forums.phpfreaks.com/topic/41726-private-messaging-system/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.