Spit_Me_Out
12-23-2006, 12:04 AM
Here is a PHP tutorial I wrote up. Anyone can use it if they wish. It teaches how to create simple PHP shoutbox for your website. Enjoy:
Firstly, you need to have acess to a MySQL Database, and something like phpMyAdmin.
Now, in phpMyAdmin, create a new database. Name this database "news". You can name it anything you want, but if you are new to PHP, it will be easier to follow if you use the same names and titles as I do.
Inside your new database called "news" create a new table and name it "shoutbox" to create the new table, click on the icon marked SQL, and copy and paste this code in it.
CREATE TABLE `shoutbox` (
`id` int(11) NOT NULL auto_increment,
`name` text NOT NULL,
`message` longtext NOT NULL,
`time` text NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;
After you have the table created we won't need to go back into your database. The rest of the script can be put on one single page.
Here are the next things we are going to do.
1. Connect to the database
2. When submit is hit, we need to get the day and time to be stored on the database.
3. Insert values of ID, name, comments, and time to database.
<?
//the host, name, and password for your mysql
mysql_connect("localhost","username","password");
//selects the database news
mysql_select_db("news");
if($submit)
{
// the PHP date function for the time
$time=date("h:ia d/j/y");
$result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,message,time)".
"VALUES ('NULL','$name', '$message','$time')");
}
?>
This next part, will display all of the comments and stuff that users have submitted to the shoutbox.
1. Selects last 5 messages posted in shoutbox table
2. Runs while loop while it keeps getting a row
3. define variables from the database, and the echo all of them.
Here's the PHP
<?
$result = mysql_query("select * from shoutbox order by id desc limit 5");
while($r=mysql_fetch_array($result))
{
//getting each variable from the table
$time=$r["time"];
$id=$r["id"];
$message=$r["message"];
$name=$r["name"];
?>
<? echo $time ?><br>
<? echo $name ?><br>
<? echo $message ?><br>
<? } ?>
Lastly, we need to write the code for the forms that the users will use to imput whatever they are going to write and submit to the shoutbox. This will seem simple compared to the rest of the tutorial.
<form action="<? echo $php_self ?>" method="post">
<INPUT TYPE='TEXT' value='name' NAME='name' SIZE=25 maxlength='100'><br>
<INPUT TYPE='TEXT' value='message' NAME='message' SIZE=50 maxlength='500'>
<input type="submit" name="submit" value="submit">
</form>
There you go, a simple shoutbox. Hope you like.
And if you see any mistakes in it, could you please tell me so that I can fix is as soon as possible.
Firstly, you need to have acess to a MySQL Database, and something like phpMyAdmin.
Now, in phpMyAdmin, create a new database. Name this database "news". You can name it anything you want, but if you are new to PHP, it will be easier to follow if you use the same names and titles as I do.
Inside your new database called "news" create a new table and name it "shoutbox" to create the new table, click on the icon marked SQL, and copy and paste this code in it.
CREATE TABLE `shoutbox` (
`id` int(11) NOT NULL auto_increment,
`name` text NOT NULL,
`message` longtext NOT NULL,
`time` text NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;
After you have the table created we won't need to go back into your database. The rest of the script can be put on one single page.
Here are the next things we are going to do.
1. Connect to the database
2. When submit is hit, we need to get the day and time to be stored on the database.
3. Insert values of ID, name, comments, and time to database.
<?
//the host, name, and password for your mysql
mysql_connect("localhost","username","password");
//selects the database news
mysql_select_db("news");
if($submit)
{
// the PHP date function for the time
$time=date("h:ia d/j/y");
$result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,message,time)".
"VALUES ('NULL','$name', '$message','$time')");
}
?>
This next part, will display all of the comments and stuff that users have submitted to the shoutbox.
1. Selects last 5 messages posted in shoutbox table
2. Runs while loop while it keeps getting a row
3. define variables from the database, and the echo all of them.
Here's the PHP
<?
$result = mysql_query("select * from shoutbox order by id desc limit 5");
while($r=mysql_fetch_array($result))
{
//getting each variable from the table
$time=$r["time"];
$id=$r["id"];
$message=$r["message"];
$name=$r["name"];
?>
<? echo $time ?><br>
<? echo $name ?><br>
<? echo $message ?><br>
<? } ?>
Lastly, we need to write the code for the forms that the users will use to imput whatever they are going to write and submit to the shoutbox. This will seem simple compared to the rest of the tutorial.
<form action="<? echo $php_self ?>" method="post">
<INPUT TYPE='TEXT' value='name' NAME='name' SIZE=25 maxlength='100'><br>
<INPUT TYPE='TEXT' value='message' NAME='message' SIZE=50 maxlength='500'>
<input type="submit" name="submit" value="submit">
</form>
There you go, a simple shoutbox. Hope you like.
And if you see any mistakes in it, could you please tell me so that I can fix is as soon as possible.