|
|
Very Simple Shoutbox - Add a very simple shoutbox to your site with PHP and MySQL | ||
Discussion by 8ennett with 2 Replies.
Last Update: March 19, 2011, 5:42 pm | |||
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:
<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:
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.
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:
CODE
<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:
CODE
<?phpmysql_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.
Mon Feb 1, 2010 Reply New Discussion
Please help!
I do this, but it returns this error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /users/yonizato/public_html/1/sh/shoutBox.php on line 9
I do this, but it returns this error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /users/yonizato/public_html/1/sh/shoutBox.php on line 9
Sun Feb 13, 2011 Reply New Discussion
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.
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.
Sat Mar 19, 2011 Reply New Discussion
Simple Chat System Create your own basic chat system using PHP! (6)
|
(3) More Advanced Shoutbox A more advanced shoutbox with fewer files and no refresh needed
|
Index




