FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Tutorial: Basic Shoutbox w/ MySQL





grim.r34p3rx
This is a huge tutorial on creating a shoutbox, with a name, URL/ Link and message. But it will also record the IP and date.

First off, we'll need a database, to hold the data in. Open PHPmyAdmin, or any other MySQL Management Software, and run this query:
Code:
CREATE TABLE `shoutbox` ( `id` INT NOT NULL AUTO_INCREMENT ,
`ip` TEXT NOT NULL ,
`name` TEXT NOT NULL ,
`url` TEXT NOT NULL ,
`message` TEXT NOT NULL ,
`date` TEXT NOT NULL ,
PRIMARY KEY ( `id` ) ) TYPE = MYISAM


This creates a table called "shoutbox", with the 6 fields. ID is the ID of the shout, we'll need this to order them later. The IP is if you want to ban someone who repeatedly posts profanity (Ban through cPanel), the Name to show who shouted, URL will link to (If they have a website), Message (self explanitory), and the date.

Note: If you already have a database connection page, you can skip this part
This will be a page to connect to the database, to get all the shouts. Name it connect.php.
Code:
<?php
$mysql_host = "localhost";
$mysql_user = "database_user";
$mysql_pass = "database_password";
$mysql_data = "database_name";
$mysql_table = "shoutbox";
mysql_connect($mysql_host,$mysql_user,$mysql_pass) or die("Couldnt connect, try again. " . mysql_error());
mysql_select_db($mysql_data) or die("Cannot select database! Please Try again.
" . mysql_error());
?>


Now we'll need a page to display all the shouts, names and links. Call this show.php:
Code:

<?
include("connect.php");
$query = mysql_query("SELECT *
FROM `shoutbox`'
ORDER BY `id` DESC
LIMIT 0 , 20 ");
if(mysql_error()) {
print(mysql_error());
}
while($row = mysql_fetch_array($query)) {
echo "<b><a href=\"".$row['link']."\" title=\"".$row['name']." posted on ".$row['date']."\">".$row['name']."</b></a >:<br />
".$row['message']."";
}
?>


Now a page to add the data typed in into the database. Name this file process.php.

Code:
<?
include("connect.php");

if ($_POST['submit']) {

$name = $_POST['name'];
$url = $_POST['url'];
$message = $_POST['message'];
$date = date('jS \o\f F, Y \a\t g\:iA');
$ip = $_SERVER['REMOTE_ADDR'];

if (!$name || !$message) {
die ('You left a field blank. Please check again.');
}
else {
mysql_query("INSERT INTO shoutbox (id,name,url,message,date,ip) VALUES('','$name','$url','$message','$date','$ip')") or die('Error inserting into DB.');
echo "Tag Added. ";
}
}
?>


Now the actual code to display the posts and the little form. This will just be the form, with a small iframe above it.

Code:
<form name="shout" method="post" action="process.php">
<div align="center"><iframe height="200" width="120" src="show.php" name="shoutbox"></iframe>
<br>
Name:<br>
<input name="name" type="text" size="15" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;">
<br>
URL:<br>
<input name="url" type="text" size="15" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;">
<br>
Message:<br>
<textarea name="message" cols="15" wrap="VIRTUAL" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;"></textarea>
<br>
<input type="submit" name="Submit" value="Submit" style="border: solid 1px #000000; font: Verdana; font-size: 10px; background: #f8f8f8;">
</div></form>


And thats it! Upload all files, and insert the last box where you want your iframe to display. Muck about with iFrame sizes and stuff if you want.
Related topics
Reply to topic    Frihost Forum Index -> Miscellaneous -> Tutorials

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.