How to create a ShoutBox using PHP and mysql

Source: Internet
Author: User
How to create a ShoutBox with PHP and mysql, read how to create a ShoutBox with PHP and mysql, as a PHP developer, I am sometimes asked to create a shoutbox. If the same thing happens to you, here is a quick guide. Obviously, you need to add your own CSS to it, but here is the basic idea. We need a MyS "> <LINKhref

As a PHP developer, I am sometimes asked to create a shoutbox. If the same thing happens to you, here is a quick guide. Obviously, you need to add your own CSS to it, but here is the basic idea.
We need a MySQL database table and three php files.
First, we need a file to save the database information.

--- File #1: mysql. inc. php ---


 
# Simply Shouting-ashoutboxexample
# File name: mysql. inc. php
# Description: A file to hold database info.
$ Host = 'localhost ';
$ User = 'database _ user_name ';
$ Password = 'database _ user_password ';
$ Name = 'database _ name ';
?>
 


Create a data table with four fields. we name it shouts. you may not have this SQL file before. create a php file "install. php ". after this file is used once, remember to delete it!

-- File #2: install. php --


 
# Simply Shouting-ashoutboxexample
# File name: install. php
# Description: Creates the database table.

// Include the database info file
Include ("mysql. inc. php ");

// Connect to the database

$ Connection = @ mysql_connect ($ host, $ user, $ password) or die (mysql_error ());
$ Db = @ mysql_select_db ($ name, $ connection) or die (mysql_error ());

// If we already have a table named "shouts", delete it first.
$ SQL = 'drop TABLE IF exists' shouts '';
$ Result = @ mysql_query ($ SQL, $ connection) or die (mysql_error ());

// Create a table without the same name
$ SQL = 'create TABLE 'shouts '(
'Id' int (11) not null auto_increment,
'Timestamp' timestamp not null default CURRENT_TIMESTAMP,
'Shoutby' varchar (50) default NULL,
'Shout' varchar (50) default NULL,
Primary key 'id' ('id ')
) TYPE = MyISAM AUTO_INCREMENT = 1 ';
Echo 'creating table: \ 'shouts \'....';
// Close the connection
$ Result = @ mysql_query ($ SQL, $ connection) or die (mysql_error ();?>


Simply Shouting-install




Your installation process has been completed. please immediately delete all the installation files from your server. This program contains the following installation files:



1) install. php






Click here to start.
 


This is the main file:

--- File #3: index. php ---


 
# Simply Shouting-ashoutboxexample
# File name: index. php
# Description: Main page to display our shouts.

// Contains database information
Include_once ("mysql. inc. php ");
// Connect to the database
$ Connection = @ mysql_connect ($ host, $ user, $ password) or die (mysql_error ());
$ Db = @ mysql_select_db ($ name, $ connection) or die (mysql_error ());
?>




// Display the latest 10 messages. first, initialize a counter.
$ Counting = 0;

// We need a counter because I want to show our shouts in ASC order
// (Like a chat room)

$ SQL = mysql_query ("SELECT * FROM 'shouts '");
While ($ data = mysql_fetch_array ($ SQL )){
// Counts every row
$ Counting = $ counting + 1;
} // End while

// If the count comes back greater than 10, then we select the last
// 10 shouts for display.

If ($ counting> 10 ){
$ Countlessten = $ counting-9;
$ SQL = mysql_query ("SELECT * FROM 'shouts 'Order BY 'shouts '. 'id' ASC LIMIT $ countlessten, 10 ");
} Else {
// Else it doesn' t matter, there's less than 10!
$ SQL = mysql_query ("SELECT * FROM 'shouts 'Order BY 'shouts '. 'id' ASC LIMIT 10 ");
}
While ($ data = mysql_fetch_array ($ SQL )){
// My timestamp field in the database is basically useless to me unless
// I parse it. The following code parses the timestamp into things I
// Can use.
$ Timestamp = $ data ['timestamp'];
$ Postedyear = substr ($ timestamp, 0, 4 );
$ Postedmonth = substr ($ timestamp, 5, 2 );
$ Postedday = substr ($ timestamp, 8, 2 );
$ Postedtime = substr ($ timestamp, 11, 5 );
$ Newpostedtime = "";
$ Nomilitary = substr ($ postedtime, 0, 2 );

// The hour is greater than 12, so we need to switch back to 1-12 and
// Add a "pm"
 
If ($ nomilitary> = 13 ){
$ Nomilitary = $ nomilitary-12;
$ Newpostedtime = $ nomilitary;
$ Newpostedtime. = ":";
$ Newpostedtime. = substr ($ postedtime, 3, 2 );
$ Newpostedtime. = "pm ";
}
If ($ newpostedtime! = ""){
$ Postedtime = $ newpostedtime;
} Else {
$ Postedtime. = "am ";
}
// Now that we have the time, let's get the shout and the shouter

$ Shoutby = $ data ['shoutby'];
$ Shout = $ data ['shout '];
 
Echo $ postedmonth. "/". $ postedday. "/". $ postedyear. "at". $ postedtime ."-". $ Shoutby." said:". $ Shout ."

";
// Looks like: 12/1/2008 at 5:02 pm-Josh said: Yo!
}
// Below is the HTML form for creating the shouts
?>




Finally, we need a PHP file processing form.

-- File #4: newshout. php --

 
# Simply Shouting-ashoutboxexample
# File name: newshout. php
# Description: Process the HTML form on index. phpand redirect.

// Obtain the name of the message recipient
$ Shoutby = $ _ POST ['shoutby'];
If ($ shoutby = "Enter your name here" | $ shoutby = ""){
// If no name is entered
$ Shoutby = "Visitor ";
}
If ($ _ POST ['shout ']) {
// Message
If ($ _ POST ['shout ']! = "Click & Shout! "){
// They didn't shout the default, so continue processing
$ Shout = $ _ POST ['shout '];
// Replace "<" and ">" to prevent hackers
$ Shout = str_replace ("<", "", $ shout );
$ Shout = str_replace (">", "", $ shout );
// Contains data information
Include_once ("dbaccess. php ");

// Connect to the database
$ Connection = @ mysql_connect ($ host, $ user, $ password) or die (mysql_error ());
$ Db = @ mysql_select_db ($ name, $ connection) or die (mysql_error ());

// Insert the message to the database
$ SQL = "INSERT INTO 'shouts' ('shoutby', 'shout') VALUES ('$ shoutby', '$ Shout ')";
// Close the connection
$ Result = @ mysql_query ($ SQL, $ connection );
}
}
?>





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.