Private Messaging system? *UPDATE*
-
- Posts: 8
- Joined: Sat Apr 24, 2010 7:25 pm
Private Messaging system? *UPDATE*
I've already made a table
CREATE TABLE `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=21;
How would I go about making my messaging system functional?
CREATE TABLE `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=21;
How would I go about making my messaging system functional?
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Private Messaging system? *UPDATE*
Here is the code I PM'd you. Hopefully if will be helpful for other users wanting to do the same thing:
You will also need to make sure you are storing the player's name in a session variable called $_SESSION['playername'] and change the link to login.php to whatever your login page is. Also, you need to change the message_read field to a boolean so it holds true/false values (although you could just keep it a single-digit int or a byte, since boolean is just an alias for 0 and 1).
This will be the page they link to when they click to check their messages:
This should work. Hopefully with the comments you should be able to see what is going on. I can't really test it out without building the db and supporting code.
Ok, this should work then. Only problem is that I use "mysqli" instead of "mysql" database extensions, and I can't remember the specifics of using the old one, like "mysql_query". I think the only difference is that you don't have to give it a link to the database, but I can't remember, so I am going to use mysqli. If you use "mysql", you should consider updating to mysqli, as it is much faster and has more features.HDWWEClips wrote:Could you help me create a PM system?
I already made a table
CREATE TABLE `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=21;
You will also need to make sure you are storing the player's name in a session variable called $_SESSION['playername'] and change the link to login.php to whatever your login page is. Also, you need to change the message_read field to a boolean so it holds true/false values (although you could just keep it a single-digit int or a byte, since boolean is just an alias for 0 and 1).
This will be the page they link to when they click to check their messages:
Code: Select all
<?php
include 'connect.php';
session_start();
//check to make sure player is logged in and store session variable in local variable if they are
if (!$_SESSION['playername']) {
echo "You are not logged in. Please go back and log in.";
echo "<a href='login.php'>Log in</a>";
} else {
$player = $_SESSION['playername'];
}
?>
<table border="2" cellpadding="3" cellspacing="0">
<tr>
<th>From</th>
<th>Subject</th>
<th>Message</th>
</tr>
<?php
//pull messages from the database
if ($result = mysqli_query($db, "SELECT * FROM messages WHERE to_user='$player'") {
//fetch each message from the associative array one by one
while ($row = mysqli_fetch_row($result)) {
//find out if the message is read, and bold it if not
if (!$row[5]) {
echo "<b>";
}
//turn PHP off to create the rest of the table
?>
<tr>
<td><?php echo($row[1]); ?></td>
<td><?php echo($row[3]); ?></td>
<td><?php echo($row[4]); ?></td>
</tr>
<?php //turn PHP back on to unbold if needed and finish up script
if (!$row[5]) {
echo "</bold>";
}
//turn off PHP to end the table
?>
</table>
<?php
//marking all messages as read
if (!($updtd = mysqli_query($db, "UPDATE messages SET message_read = 'true' WHERE to_user='$player'"))) {
echo "Unable to set messages as read.";
}
}
} else {
echo "You have no messages.";
}
?>
The indelible lord of tl;dr
-
- Posts: 8
- Joined: Sat Apr 24, 2010 7:25 pm
Re: Private Messaging system? *UPDATE*
doesnt work
parse error on line 22

parse error on line 22
-
- Posts: 8
- Joined: Sat Apr 24, 2010 7:25 pm
Re: Private Messaging system? *UPDATE*
Members table
CREATE TABLE `tz_members` (
06
`id` int(11) NOT NULL auto_increment,
07
`usr` varchar(32) collate utf8_unicode_ci NOT NULL default '',
08
`pass` varchar(32) collate utf8_unicode_ci NOT NULL default '',
09
`email` varchar(255) collate utf8_unicode_ci NOT NULL default '',
10
`regIP` varchar(15) collate utf8_unicode_ci NOT NULL default '',
11
`dt` datetime NOT NULL default '0000-00-00 00:00:00',
12
PRIMARY KEY (`id`),
13
UNIQUE KEY `usr` (`usr`)
14
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Notice that we’ve defined the id as an integer with auto_increment – it is automatically
CREATE TABLE `tz_members` (
06
`id` int(11) NOT NULL auto_increment,
07
`usr` varchar(32) collate utf8_unicode_ci NOT NULL default '',
08
`pass` varchar(32) collate utf8_unicode_ci NOT NULL default '',
09
`email` varchar(255) collate utf8_unicode_ci NOT NULL default '',
10
`regIP` varchar(15) collate utf8_unicode_ci NOT NULL default '',
11
`dt` datetime NOT NULL default '0000-00-00 00:00:00',
12
PRIMARY KEY (`id`),
13
UNIQUE KEY `usr` (`usr`)
14
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Notice that we’ve defined the id as an integer with auto_increment – it is automatically
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Private Messaging system? *UPDATE*
not sure if this is the issue but you are missing a ) in
also I would not veer away from the format the videos use. this is where most people get confused. Keep one format and stick with it otherwise you will get lost easy
may need to proofread it, i did it in a hurry, also why do you need a auto increment message counter? That could get very large when lots of players start sending messages.
Code: Select all
if ($result = mysqli_query($db, "SELECT * FROM messages WHERE to_user='$player'") {
Code: Select all
echo "<small>";
echo "<center>";
echo "<table border='0' width='70%' cellspacing='20'>";
echo "<tr><td width='25%' valign='top'>";
echo "</td>";
echo"<td valign='top' width='75%'>";
$messageinfo="SELECT * FROM messages WHERE to_user='$player'";
$messageinfo2=mysql_query($messageinfo) or die("could not select messages.");
echo"<table border='1' bordercolor='black' >";
echo "<tr><td>From</td><td>Subject</td><td>Message</td><td></td></tr>";
while($messageinfo3=mysql_fetch_array($messageinfo2))
{
if($messageinfo3['message_read'] == 1)
{
$messageread = "New!";
}
else
{
$messageread = "";
}
echo "<tr><td>$messageinfo3[from_user]</td><td>$messageinfo3[message_title]</td><td>$messageinfo3[message_contents]</td><td>$messageread</td></tr>";
}
print "</table>";
print "</td></tr></table>";
print "</center>";
echo "</small>";
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Private Messaging system? *UPDATE*
The missing parenthesis is definitely an error, it not the error. I did not have time to make the db, supporting code and message writing page to try it out myself, so all I cold do was eyeball it a couple of times.hallsofvallhalla wrote:not sure if this is the issue but you are missing a ) inalso I would not veer away from the format the videos use. this is where most people get confused. Keep one format and stick with it otherwise you will get lost easyCode: Select all
if ($result = mysqli_query($db, "SELECT * FROM messages WHERE to_user='$player'") {
may need to proofread it, i did it in a hurry, also why do you need a auto increment message counter? That could get very large when lots of players start sending messages.Code: Select all
echo "<small>"; echo "<center>"; echo "<table border='0' width='70%' cellspacing='20'>"; echo "<tr><td width='25%' valign='top'>"; echo "</td>"; echo"<td valign='top' width='75%'>"; $messageinfo="SELECT * FROM messages WHERE to_user='$player'"; $messageinfo2=mysql_query($messageinfo) or die("could not select messages."); echo"<table border='1' bordercolor='black' >"; echo "<tr><td>From</td><td>Subject</td><td>Message</td><td></td></tr>"; while($messageinfo3=mysql_fetch_array($messageinfo2)) { if($messageinfo3['message_read'] == 1) { $messageread = "New!"; } else { $messageread = ""; } echo "<tr><td>$messageinfo3[from_user]</td><td>$messageinfo3[message_title]</td><td>$messageinfo3[message_contents]</td><td>$messageread</td></tr>"; } print "</table>"; print "</td></tr></table>"; print "</center>"; echo "</small>";
As far as the video vs. my code, for future videos you should change from mysql extension to mysqli. Mysql extension is up for deprecation in the next full version update of PHP, and mysqli has better performance and supports all of the more recent features of MySQL whereas mysql extension doesn't. Although I do agree that folks shouldn't mix styles. I just didn't remember how to use the mysql extension, although I looked it up later and for those commands used here, the only difference is that you don't have to provide a database resource ($db) as the first parameter.
The indelible lord of tl;dr
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Private Messaging system? *UPDATE*
yeah wasn't saying your code was wrong or anything, I just didn't want to confuse him
either way is fine.

- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Private Messaging system? *UPDATE*
It would probably be cleaned to just echo all of those table HTML elements or use another HTML-in-PHP solution rather just jumping in and out of PHP, but I happened to have an older page I wrote that I was able to copy and paste for the general structure to start editing. If I wrote it all over from scratch I would probably do it differently to keep it more elegant.
The indelible lord of tl;dr
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Private Messaging system? *UPDATE*
i have gotten into the habit(don't know if it is bad or good) to echo out nearly all my HTML. Probably a little harder on the server but its easier on me.