View Full Version : php comment box

07-25-2006, 03:07 AM
i have this code but i am not sure where to start with it. i am new to php and i need some advice. any help will be appreciated. thanks guys.


07-25-2006, 07:54 AM
i assume you have installed MYSL and PHP on your system and that all works well

first create your MYSQL database...

you can run this from the command line...

CREATE TABLE `phpshout_shouts` (
`nick` varchar(255) NOT NULL default '',
`date` varchar(255) NOT NULL default '',
`shout` longtext NOT NULL,
`id` tinyint(11) NOT NULL auto_increment,

now create you main php script in a blank page with a PHP extention... (paste the code below into that page)

$query = "SELECT * FROM phpshout_shouts ORDER BY id DESC" ;
$result = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_num_rows($result) == 0){
echo("<table border='0' width='100%' cellspacing='0' cellpadding='0' id='table1'>");
echo("<tr class='table2'>");
echo("<a>There is nothing in the shoutbox, be the first to shout something!</a>");
echo("<table border='0' width='100%' cellspacing='1' cellpadding='0' id='table1'>");
echo("<tr><td class='table1'><a>Posted by $nick</a></td></tr><tr><td class='table2'><a>Posted $date</a></td></tr><tr><td class='table3'><a> $shout <hr></a></td></tr>");

<form action="submit_shout.php" method="post">
<table border="0" width="100%" cellspacing="0" cellpadding="0" id="post">
<tr class="table1">
<td><input type="text" name="nick" size="14" value="nickname"></td>
<tr class="table2">
<font face="Arial">
<textarea name="shout" cols="11" rows="3" id="shout">message </textarea></font>

<input type="submit" value="submit" style="font-family: Verdana; font-size: 8pt; background-color: #666666; color:#FFFFFF" name="submit"><input type="reset" value="reset" style="font-family: Verdana; font-size: 8pt; background-color: #666666; color:#FFFFFF" name="reset"></td>

as you can see from the code above there are includes these could be looked at as libraries in other code syntax like C or JAVA

now create your CSS...

body {
background-color: #333333;

a {
font: 8pt "Verdana";
font-color: #FFFFFF;
h1 {
font: 14pt "Verdana";
color: White;

.title {
font: 8pt "Verdana";
font-color: #FFFFFF;
font-weight: bold;

.date {
font: 8pt "Verdana";
font-color: White;

.table1 {
background-color: #666666;
color: #FFFFFF;

.table2 {
background-color: #999999;
color: #000000;
.table3 {
background-color: #e0e0e0;
color: #000000;

link that CSS into your main page as normal

Then make a folder called include and make a file called header.php and a file footer.php.

In header.php add this code:

echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'";
echo "'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
echo "<html xmlns='http://www.w3.org/1999/xhtml'>";
echo "<head>";
echo "<meta http-equiv='Content-Language' content='en-au'>";
echo "<link rel='stylesheet' type='text/css' href='style.css'>";
echo "<meta http-equiv='Content-Type' content='text/html; charset=windows-1252'>";
echo "<title>";
echo ".::Shoutbox::.";
echo "</title>";
echo "</head>";
echo "<body>";

And for footer.php:

echo "</body>";
echo "</html>";

You should then make a file called submit_shout.php in the main shoutbox folder and give it this code below

echo "<HTML>";
echo "<HEAD>";
echo "<META HTTP-EQUIV='Refresh' CONTENT='2; URL=index.php'>";
echo "<TITLE>";
echo "redirecting";
echo "</TITLE>";
echo "</HEAD>";
echo "<BODY>";
$x = "";
$time = date('D M d, Y ') ;
$time2 = date("H")+$x . date(":i") ;
$s = " ";
$nick = htmlspecialchars($nick);
$text = htmlspecialchars($text);
mysql_query("INSERT INTO phpshout_shouts
(date,nick,shout) VALUES('$time', '$nick', '$text' ) ")
or die(mysql_error());
echo "Added.....Redirecting Back";
echo "</BODY>";
echo "</HTML>";

The last step is to make a db_settings.php file inside the include folder and add the code below. Then you should customize the password etc,

$username = "root";
$password = "";
$host = "localhost";
$database = "shoutbox";
mysql_connect($host,$username,$password) or die("Error!, Could not connect to Database, please read the documentation!
" . mysql_error());
mysql_select_db($database) or die("Could not select database, make sure it exists!
" . mysql_error());