Tạo 1 cái Shoutbox đơn giản
Bạn làm theo các bước sau đây.
B1: Tạo 1 DB như sau:
PHP Code:
CREATE TABLE `tagboard` (
`id` int(7) NOT NULL auto_increment,
`name` varchar(250) NOT NULL default '',
`comments` text NOT NULL,
`date` timestamp(14) NOT NULL,
PRIMARY KEY (`id`),
) TYPE=MyISAM AUTO_INCREMENT=419 ;
B2:Viết 1 file config.php có nội dung như sau:
PHP Code:
<?
$dbuser=""; //Database Username
$dbname=""; //Database Name
$dbpass=""; // Database Password
?>
B3:Tạo 1 file tag.php có nôi dung như sau:
PHP Code:
<iframe src="view.php" name="tag" width="179" height="130" frameborder=0 marginwidth="0" marginheight="0"></iframe><form method="POST" ACTION="todo.php">
<input type="text" NAME="name" Value=">">
<textarea NAME="post" cols="27" rows="3"></textarea>
<br>
<input TYPE="submit" value="Submit">
B4:Tiếp theo là file todo.php
PHP Code:
<?php
if ($name == '' || $post == ''")
{
die ("Please fill all fields. Click <a HREF=tag.php>here</a> to return.");
}
include('config.php');
$post = preg_replace("/</","<",$post);
$post = preg_replace("/>/",">",$post);
$post = nl2br($post);
$comments = "$post";
}
$c=mysql_connect("localhost","$dbuser","$dbpass");
mysql_select_db($dbname);
$todo="INSERT INTO tagboard (id,name,comments,date) VALUES('','$name','$comments',now())";
$solution = mysql_query($todo) or die (mysql_error());
if ($solution)
{
?>
<meta http-equiv="refresh" content="0;url=tag.php" >
<?
} ?>
B5:Cuối cùng là file view.php
PHP Code:
<?php
include('config.php');
//Kết nối DB
$c=mysql_connect("localhost","$dbuser","$dbpass");
//Lựa chọn DB
mysql_select_db($dbname);
//Lựa chọn tên DB
$todo='SELECT * FROM `tagboard` order by id desc LIMIT 50';
$solution=mysql_query($todo);
while ($place = mysql_fetch_array($solution))
{
$id=$place***91;"id"***93;;
$comments=$place***91;"comments"***93;;
//là nơi hiển thị trên trang web
?>
»<b><? echo "$place***91;name***93;"; ?></b><? echo ": $comments<br>" ; ?>
<?
}?>
Chúc bạn thành công !
Bookmarks