Skip to main content

PHP/MySQLi Messaging System

This tutorial will be going over a basic PHP messaging system between users.

First you will need a host for your PHP, either a web host or localhost is fine but you will need PHP and MySQL(i) capabilities.

Also, this will not be covering creating users.

We are going to be storing all of the messages in a database named 'messagesTutorial', and in there a table named 'messages'. Let's create these now;

Go to PHPMyAdmin and enter 'messagesTutorial' followed by pressing the 'create' button. Once it has created, click on the database and add a table, put the following information;
4 Columns:
id - INT - 5 Length - Primary Key - Auto Increment (AI/A_I).
message - VARCHAR - 255 Length.
to - VARCHAR - 255 Length.
from - VARCHAR - 255 Length.

Now we are ready to work on the HTML Forms.

Send Messages:
First we want a form to send a message to another user and populate our database. For this, all we need is a description of the message, the username from and the username to [You can add things such as dates, etc.]...

  1. <html>
  2. <head></head>
  3. <body>
  4. <h1>Send Message:</h1>
  5. <form action='messageSystem.php' method='POST'>
  6. <table>
  7. <tbody>
  8. <tr>
  9. <td>To: </td><td><input type='text' name='to' /></td>
  10. </tr>
  11. <tr>
  12. <td>From: </td><td><input type='text' name='from' /></td>
  13. </tr>
  14. <tr>
  15. <td>Message: </td><td><input type='text' name='message' /></td>
  16. </tr>
  17. <tr>
  18. <td></td><td><input type='submit' value='Create Task' name='sendMessage' /></td>
  19. </tr>
  20. </tbody>
  21. </table>
  22. </form>
  23. </body>
  24. </html>

We have set the action to messageSystem.php as we are going to save this form page as messageSystem.php, this means we only have to create and use one page - it just makes it easier for us and the user.

Next we need the PHP;

  1. <?php
  2. $con = mysqli_connect('localhost', 'root', '', 'messagesTutorial') or die(mysql_error());
  3. if (isSet($_POST['sendMessage'])) {
  4. if (isSet($_POST['to']) && $_POST['to'] != '' && isSet($_POST['from']) && $_POST['from'] != '' && isSet($_POST['message']) && $_POST['message'] != '') {
  5. $to = $_POST['to'];
  6. $from = $_POST['from'];
  7. $message = $_POST['message'];
  8. $q = mysqli_query($con, "INSERT INTO `messages` VALUES ('', '$message', '$to', '$from')") or die(mysql_error());
  9. if ($q) {
  10. echo 'Message sent.';
  11. }else
  12. echo 'Failed to send message.';
  13. }
  14. }
  15. ?>

We first check if the submit button has been sent (if the form has been used), then we check that there is a valid message from the 'message' field in our form, to username from our 'to' and from username from our 'from' values. If there is valid information we insert it in to our messages table within our database with id as nothing (due to it Auto_Incrementing through the database already), message as the form message, to as the form to and from as the form from. We finally output the end result.

You may want to replace the input of 'from' with the final value of the logged in user ($_SESSION['username']) if you have used my register and login system. I don't have either of those connected to this tutorial so I have just given the option to enter a from username through the form.

Listing Messages:
So now that the user can populate the database with messages, we need to list the users received messages;

  1. <h1>My Messages:</h1>
  2. <table>
  3. <tbody>
  4. <?php
  5. $user = 'myUsername'; //$user = $_SESSION['username'];
  6. $qu = mysqli_query($con, "SELECT * FROM `messages` WHERE `to`='$user'");
  7. if (mysqli_num_rows($qu) > 0) {
  8. while ($row = mysqli_fetch_array($qu)) {
  9. echo '<tr><td>'.$row["from"].'</td><td>'.$row["message"].'</td></tr>';
  10. }
  11. }
  12. ?>
  13. </tbody>
  14. </table>

Again, I have just set it to only show the messages to 'myUsername', I have added how it should look as a comment on the same line, replace the bit before the comment with the commented part ($user = $_SESSION['username'];) to get it to work for any user (as long as that is your login scripts session variable of their username).

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.