Previous | Next

     This is the first page of a tutorial that will show you how to create private messaging system. This tutorial has 6 pages (including this one) each of which will cover just one of the pages for the private messaging system. This first part will get you setup to make your private messaging system, because we need a table in our database to hold the messages, and if you dont know how to yet we need to be able to connect to that database from within PHP. If you don't want to read through the whole tutorial, here are all of the files in a .zip with instructions on setting it up. Private Messaging System Zip

     If you do not have a database, you need to create one. You can do that however you normally do, PHPmyadmin, or whatever. You can name it whatever you want. After you make the database, you need to select it and get to where you can run an SQL query in the database. Then run the following query.
CREATE TABLE `messages` (
`id` int(11) NOT NULL auto_increment,
`reciever` varchar(25) NOT NULL default '',
`sender` varchar(25) NOT NULL default '',
`subject` text NOT NULL,
`message` longtext NOT NULL,
`recieved` enum('1','0') default '0',

     This messaging system requires some sort of login system, if you dont have one, get one, or make one. In the login script check to see if when the sessioned username variable is created, the variable is named "username".If it is not named "username" then you need to change that in all the pages of the private messaging system to whatever it is in your login script. Once you have your login system you need to edit the table that contains the user data and add the field pm_count as an integer to it with a default value of 0, because that's how this script stores the data on how many private messages a user has.

     Ok, now that we have the table created we need to be able to connect to that database we just created, lets make a file called db.php which we will include on any page that we need to connect to the database with. Here's the code for that page, just replace the variables for username, password, and database name with the one's for your database.

//These are the variables you need to change
    //The host for your database, usually localhost will work
$dbhost 'localhost';
//The username that is running MySQL
$dbusername 'username';
//The password for the user for MySQL
$dbpasswd 'password';
//The name of the database you created
$database_name 'php_tuts';
//This part attempts to open a connection to the host of the database with the specified username and password
$connection mysql_connect("$dbhost","$dbusername","$dbpasswd"
    or die (
"Couldn't connect to server.");
//This part actually selects the database from any others on the server and lets you select tables to get data from and insert into and stuff
$db mysql_select_db("$database_name"$connection)
    or die(
"Couldn't select database.");

     One last thing real quick, if you encounter any problems during this tutorial or with code not working, PLEASE e-mail me at "bedfordd @" (Remove the spaces).

Part One - Getting Started: The first page will tell you how to set up the database and table that you will need for the Private Messaging System, and will also show you how to connect to the database you just created.

Part Two - Inbox: The second page will teach you first of all, how to make the inbox. This page will search for any message that have been sent to the logged in user, display them, let the user click a link to view them, and finally to select them for deletion.

Part Three - Delete: The third page will recieve the information from the inbox page for which private messages need to be deleted, process the information, and to actually do the deletion.

Part Four - View Messages: The fourth page will teach you how to recieve the id variable sent through the URL when you click the link on the inbox to view the message. It will double check that you are the reciever of the message, and will display the message in full.

Part Five - Compose: The fifth page will teach you how to make the form to write a message and to send the form to itself, do some error checking to make sure they didnt try to send a blank message, or a message to a non existant user, or a message to themself.

Part Six - Sent Messages: The sixth, and least useful page, will just show you which messages you have sent to other users that haven't been read or deleted yet.