How to create a shoutbox_php tutorial with PHP and MySQL

Source: Internet
Author: User
Tags php and mysql

As a PHP developer, I was sometimes asked to make a shoutbox. If the same thing happens to you, here's a quick guide. Obviously, you want to add your own CSS to it above, but here's 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 are named shouts. Previously you may not have this SQL file, create a php file "install.php". After this file has been 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");

Connecting to a 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", we need to delete it first.
$sql =drop TABLE IF EXISTS ' shouts ';
$result = @mysql_query ($sql, $connection) or Die (Mysql_error ());

Now determine the table without the same name, create it
$sql =create TABLE ' shouts ' (
' id ' int (one) not NULL auto_increment,
' Timestamp ' timestamp not NULL DEFAULT Current_timestamp,
' shoutby ' varchar default NULL,
' Shout ' varchar default NULL,
PRIMARY KEY ' id ' (' ID ')
) Type=myisam auto_increment=1;
Echocreating table:shouts ...;
Close connection
$result = @mysql_query ($sql, $connection) or Die (Mysql_error ());? >


Simply Shouting-Installation




Your installation process is complete. Please remove all installation files from your server immediately. 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");
Connecting to a database
$connection = @mysql_connect ($host, $user, $password) or Die (Mysql_error ());
$db = @mysql_select_db ($name, $connection) or Die (Mysql_error ());
?>




Showing the latest 10 comments. First, initialize a counter
$counting = 0;

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

$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 and then we select the last
Ten 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 doesnt matter, theres 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 was basically useless to me unless< br>//i Parse it. The following code parses the timestamp to things I
//can use.
$timestamp = $data [timestamp];
$postedyear =subs TR ($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, 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 then we have the time, lets 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:02pm-josh said:yo Yo yo!
}
Below is the HTML form for creating the shouts
?>




Finally, we need a PHP file to process the form.

--File #4:newshout.php--


!--? # Simply shouting-ashoutboxexample
# File name:newshout.php
# description:process the HTML form on Index.phpand redirect.

Name of the person who received the message
$shoutby =$_post[shoutby];
if ($shoutby = = "Enter your name Here" | | $shoutby = = "") {
If no name is entered
$shoutby = "Visitor";
}
if ($_post[shout]) {
Message message
if ($_post[shout]! = "click & shout!") {
They didnt shout the default, so continue processing
$shout =$_post[shout];
Replace "<" and ">" to stop hackers
$shout =str_replace ("<", "", $shout);
$shout =str_replace (">", "", $shout);
Contains data information
Include_once ("dbaccess.php");

Connecting to a database
$connection = @mysql_connect ($host, $user, $password) or Die (Mysql_error ());
$db = @mysql_select_db ($name, $connection) or Die (Mysql_error ());

Inserting message information into the database
$sql = "INSERT into ' shouts ' (' shoutby ', ' shout ') VALUES ($shoutby, $shout)";
Close connection
$result = @mysql_query ($sql, $connection);
}
}
?>





http://www.bkjia.com/PHPjc/508218.html www.bkjia.com true http://www.bkjia.com/PHPjc/508218.html techarticle as a PHP developer, I was sometimes asked to make a shoutbox. If the same thing happens to you, here's a quick guide. Obviously, you want to add your own ...

  • 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.