UK Webmaster Talk - Online Marketing - SEO


 

How to make a shoutbox in PHP/Mysql

This is a discussion on How to make a shoutbox in PHP/Mysql within the Programming Articles forums, part of the Webmaster Articles/Tutorials category; In this tutorial, you will make a shoutbox using PHP/MYSQL. First, create a new database or use an existing one ...


Go Back   UK Webmaster Talk - Online Marketing - SEO > Webmaster Articles > Webmaster Articles/Tutorials > Programming Articles

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Notices

Reply

 

LinkBack Thread Tools Display Modes
Old 07-04-2005, 16:10   #1 (permalink)
Bmg
WMT Addict
 
Join Date: Apr 2005
Posts: 227
iTrader: 0 / 0%
Bmg is on a distinguished road
Default How to make a shoutbox in PHP/Mysql

In this tutorial, you will make a shoutbox using PHP/MYSQL.

First, create a new database or use an existing one & place the following into the sql query tab:

CREATE TABLE `shoutbox` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`message` TEXT NOT NULL,
`author` VARCHAR(80) NOT NULL,
`email` VARCHAR(50) NOT NULL,
`date` VARCHAR(50) NOT NULL,
`ip` VARCHAR(20) NOT NULL ,
PRIMARY KEY (`id`))

Now create a new document called shoutbox.php & place the following code into it & replace the connection details to match your sites:

<?php
// You just need to configure these 4 variables to match your server.
$db_host = "localhost"; // mySQL database host
$db_user = "username"; // mySQL database user
$db_password = "password"; // mySQL database password
$db_name = "database"; // the name of your mySQL database
// If a user has submitted a post, we want to :
// 1. Validate it
// 2. Strip unwanted html
// 3. Make sure messages and names aren't too long
// 4. Add it to our database.
if($_POST['submit']) {
// 1. Validate it, by checking all the form inputs were filled in
if(!$_POST['author']) {
echo 'Error ! : No name entered';
die;
}
if(!$_POST['email']) {
echo 'Error ! : No email entered';
die;
}
if(!$_POST['message']) {
echo 'Error ! : No message entered';
die;
}
// 2. Strip unwanted HTML
// Look up the strip_tags() function at
// http://www.php.net/manual/en/function.strip-tags.php for more info
$message = strip_tags($_POST['message'], '');
$email = strip_tags($_POST['email'], '');
$author = strip_tags($_POST['author'], '');
// 3. Make sure messages and names aren't too long
// We will use the strlen() function to count the length.
$message_length = strlen($message);
$author_length = strlen($author);
if($message_length > 150) {
echo "Error ! : Your message was too long, messages must be less than 150 chars";
die;
}
if($author_length > 150) {
echo "Error ! : Your name was too long, names must be less than 150 chars";
die;
}
// 4. Add it to our database.
// If the script hasn't died yet due to an error in the inputted data
// we need to add the data to the database
// Lets connect to our database.
mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());
// Select the database.
mysql_select_db($db_name) or die(mysql_error());
// Lets define the date format we want to enter to our database
// go here for more details
// http://www.php.net/manual/en/function.date.php
$date = date("h:i A dS M");
// This will produce 11:02 25th Aug
// Set the query as $query
$query = "INSERT INTO shoutbox (message, author, email, date, ip)
VALUES ('$message','$author','$email','$date','$_SERVER[REMOTE_ADDR]')";
mysql_query($query);
mysql_close();
// Show thanks message and take them back to the main shoutbox
echo "Thanks for your post<BR>";
echo "<A HREF=\"shoutbox.php\">View the shoutbox</A>";
// If they haven't submitted a post, we want to :
// 1. Show the latest shouts
// 2. Show the shout post form
} else {
// 1. Show the latest shouts
// Lets connect to our database.
mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());
// Select the database.
mysql_select_db($db_name) or die(mysql_error());
// Set the query as $query, and get the last 10 posts.
$query = "SELECT message, author, email, date, ip
FROM shoutbox order by id DESC LIMIT 10";
$result = mysql_query($query);
echo "<TABLE>";
while($r=mysql_fetch_array($result))
{
// To modify the appearance, edit this :
echo "<TR>
<TD><font size='1'>
Posted $r[date] by <A HREF=\"mailto:$r\">
$r[author]</A></font></TD>
</TR>
<TR>
<TD><font size='1'>$r[message]</font></TD>
</TR>
<TR>
<TD><HR></TD>
</TR>";

}
echo "</TABLE>";
// 2. Show the shout post form
?>
<FORM METHOD=POST ACTION="shoutbox.php">
<TABLE>
<TR>
<TD>Name :</TD>
<TD><INPUT TYPE="text" NAME="author"></TD>
</TR>
<TR>
<TD>Email :</TD>
<TD><INPUT TYPE="text" NAME="email"></TD>
</TR>
<TR>
<TD>Message :</TD>
<TD><INPUT TYPE="text" NAME="message"></TD>
</TR>
<TR>
<TD>&nbsp;</TD>
<TD><INPUT TYPE="submit" name="submit" value="post"></TD>
</TR>
</TABLE>
</FORM>
<?php
}
?>

Now, include this in your sites pages using the following:


<?php include("./shoutbox.php"); ?>
Bmg is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
make, php or mysql, shoutbox

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
PHP/HTML/MYSQL - for ??$$ KF1 Website Development Forum 0 20-07-2005 17:27
php & mysql radio hdshngout Php and MySQL 7 09-07-2005 06:09
MySql - Help / Questions. SirJinX Hosting Forum 9 04-07-2005 20:54
We need a shoutbox Ic3CoLd Comments / Suggestions 9 11-04-2005 14:46
Flash Shoutbox asgsoft Programming / Scripting / Coding 2 09-04-2005 14:34


All times are GMT +1. The time now is 18:43.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
UK Webmaster Forum © WebmasterTalk.co.uk | Design by Forbairt

Ad Management by RedTyger

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41