Jump to content
xisto Community
8ennett

Very Simple Shoutbox Add a very simple shoutbox to your site with PHP and MySQL

Recommended Posts

Ok so now we're going to make our very own shoutbox (mini chatroom) using PHP and MySQL. First we need to create a .htm file with a form for accepting a users name and message with a submit button. A seperate PHP document will connect to the database and insert the values in to the table. At the same time it will fetch the last ten records from the table and will display them on the screen. But before that we need to create a table which will store the users name, message and time the message was posted.

 

To create the table run the following MySQL query either in the console or phpMyAdmin:

 

CREATE TABLE shoutbox (

`user-id` int(11) NOT NULL auto_increment,

`user-name` text NOT NULL,

`message` longtext NOT NULL,

`time` text NOT NULL,

PRIMARY KEY (`user-id`)

) engine="innodb";

 

Now you have your MySQL table we can create the first document which is going to be called shoutBox.html document:

 

"Content-Type" content="text/html;      charset=ISO-8859-1">

<title>ShoutBox</title>

</head>

<body>

<form action="shoutBox.php" method="post" >

Name _linenums:0'><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>ShoutBox</title> </head> <body> <form action="shoutBox.php" method="post" > Name:   <input type="text" name="name" size=30 maxlength='100'></input><br/> Message: <input type="text" name="message" size=30 maxlength='100'></input><br/> <input type="submit" name="submit" value="submit"></input> </form> </body> </html>


Now create a new document and call it shoutBox.php then copy and paste the following code:

 


$message=$_POST['message'];

$time=date("h linenums:0'><?php mysql_connect("localhost","root",""); mysql_select_db("Bennett"); $name=$_POST['name']; $message=$_POST['message']; $time=date("h:ia d/j/y"); $result=mysql_query("INSERT INTO shoutbox (id,name,message,time) VALUES ('NULL','$name', '$message','$time')"); $result = mysql_query("SELECT * FROM shoutbox ORDER BY id DESC LIMIT 10 "); while($r=mysql_fetch_array($result)){ $time=$r["time"]; $id=$r["id"]; $message=$r["message"]; $name=$r["name"]; echo $name."<br/>".$message."<br/>".$time."<br/>"; } ?>


Ok now you can test it out by opening your html document in a web server. Clicking on the submit button 5 times will display the following:

 

Bennett

this is new text

11:07am 28/01/09

Bennett

this is new text

11:03am 28/01/09

Bennett

this is new text

11:03am 28/01/09

Bennett

this is new text

11:03am 28/01/09

Bennett

this is new text

11:03am 28/01/09

 

This will display up to ten messages entered in the database starting with the most recently submitted. Play around with it and find different ways of integrating it in to your own site.

Share this post


Link to post
Share on other sites

Sounds like a problem with your host, do they add a table prefix before the table name? eg. nyusername_shoutbox? If so, then when running the mysql query you will need to add the prefix to the beginning of the table name. You could also try changing while($r=mysql_fetch_array($result)){ to while($r=mysql_fetch_assoc($result)){ and see if that works. If that's not the case then make sure you are connected to the correct database through mysql_fetch_db() and this might also have a prefix on it (although that shouldn't matter) that needs including. This is a very old script, one of my first in fact and I haven't gone through and tested or debugged it. Am too busy with PGE nowadays as well to go through my old tutorials and double-check everything sorry.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.