The database in this example is very simple and you will understand it at a glance.
Demo
XML/HTML Code
-
-
-
-
-
- Latest Messages
-
-
-
-
-
Shoutbox. php
PHP Code
-
- /************************
- CONSTANTS
- /************************/
- Define ("HOST", "localhost ");
- Define ("USER", "root ");
- Define ("PASSWORD ","");
- Define ("DB ","");
-
- /************************
- FUNCTIONS
- /************************/
- Function connect ($ db, $ user, $ password ){
- $ Link = @ mysql_connect ($ db, $ user, $ password );
- Mysql_query ("set names utf8 ;");
- If (! $ Link)
- Die ("cocould not connect:". mysql_error ());
- Else {
- $ Db = mysql_select_db (DB );
-
- If (! $ Db)
- Die ("cocould not select database:". mysql_error ());
- Else return $ link;
- }
- }
- Function getContent ($ link, $ num ){
- $ Res = @ mysql_query ("SELECT * FROM content order by updatetime desc limit". $ num, $ link );
- If (! $ Res)
- Die ("Error:". mysql_error ());
- Else
- Return $ res;
- }
- Function insertMessage ($ user, $ message ){
- $ Updatetime = date ("Y-m-d H: I: s", time ());
- $ Query = sprintf ("insert into content (name, message, updatetime) VALUES ('% s',' % s', '$ updatetime ');", mysql_real_escape_string (strip_tags ($ user), mysql_real_escape_string (strip_tags ($ message )));
- $ Res = @ mysql_query ($ query );
- If (! $ Res)
- Die ("Error:". mysql_error ());
- Else
- Return $ res;
- }
-
- /******************************
- MANAGE REQUESTS
- /******************************/
- If (! $ _ POST ['action']) {
- // We are redirecting people to our shoutbox page if they try to enter in our shoutbox. php
- Header ("Location: index.html ");
- }
- Else {
- $ Link = connect (HOST, USER, PASSWORD );
- Switch ($ _ POST ['action']) {
- Case "update ":
- $ Res = getContent ($ link, 20 );
- While ($ row = mysql_fetch_array ($ res )){
- $ Result. ="
- ". $ Row ['name']."". $ Row ['message'].". $ row ['updatetime']."
";
- }
- Echo $ result;
- Break;
- Case "insert ":
-
- Echo insertMessage ($ _ POST ['Nick '], $ _ POST ['message']);
- Break;
- }
- Mysql_close ($ link );
- }
-
-
- ?>
Address: http://www.freejs.net/article_biaodan_125.html