Introduction
In this article I explain how to create a shoutbox in PHP. Creating a shoutbox using a MySQL database to store shouts in a table. First of all I will create a simple HTML file and then creating a database. In the database a table is created and the table name is "shoutbox".
Example
This is a HTML file to be saved as form.html:
<html>
<head>
<title>My Shout Box Form</title>
</head>
<body bgcolor="#FCFBDC">
<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>
This is a PHP file; save this file as "shoutbox.php". In this file is a builtin connection file. I have created a database with MySQL to save the shoutbox in our table. In this database you will need to create four fields for Id, name, message and time.
This is the table for storing the shouts:
<?php
mysql_connect("localhost","root","");
mysql_select_db("vinod");
$name=$_POST['name'];
$message=$_POST['message'];
$time= date("F j, Y, g:i a");
$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 5");
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/>";
}
?>
Output
Here, you will enter the name and message then press the submit button so your shouts data is saved in your table.
Here, show your shouts data fetched from the table.