PHP + MySQL simple message book _ PHP Tutorial

Source: Internet
Author: User
PHP + MySQL creates a simple message book. Message display page: the main code for paging and message reply is added compared with the previous example: installindex. php: Copy the code on the program installation page as follows :? Phpif ($ _ GET [acti

Message Display Page: added the paging and message reply functions compared to the previous example.

Main code:
Install/index. php: Program installation page

The code is as follows:


If ($ _ GET ["action"]! = 1)
{
?>

} Else {
@ Set_time_limit (1000 );
$ Lockfile = "install. lock ";
$ Host = "localhost ";
$ User = "root ";
$ Password = "";
$ Database = "ruizhi_messageboard ";
$ Pagenum = 10;
$ Admin_name = "admin ";
$ Admin_password = "123456 ";
$ Boardname = "RUIZHINET message book ";
If ($ _ POST ["host"]! = ""){
$ Host = $ _ POST ["host"];
}
If ($ _ POST ["user"]! = ""){
$ User = $ _ POST ["user"];
}
If ($ _ POST ["password"]! = ""){
$ Password = $ _ POST ["password"];
}
If ($ _ POST ["database"]! = ""){
$ Database = $ _ POST ["database"];
}
If ($ _ POST ["pagenum"]! = ""){
$ Pagenum = $ _ POST ["pagenum"];
}
If ($ _ POST ["admin_name"]! = ""){
$ Admin_name = $ _ POST ["admin_name"];
}
If ($ _ POST ["admin_password"]! = ""){
$ Admin_password = $ _ POST ["admin_password"];
}
If ($ _ POST ["boardname"]! = ""){
$ Boardname = $ _ POST ["boardname"];
}
If (file_exists ($ lockfile )){
Exit ("already installed. if you want to reinstall it, delete install/install. lock ");
}
$ Conn = mysql_connect ($ host, $ user, $ password );
If ($ conn ){
$ SQL _drop_database = "DROP DATABASE IF EXISTS '". $ database ."'";
$ SQL _create_database = "CREATE database'". $ DATABASE ."'";
$ SQL _create_table_messageboard = "CREATE TABLE 'messageboard '(
'Id' int (11) not null auto_increment,
'Author' varchar (15) not null,
'Title' varchar (30) not null,
'Smiles 'varchar (30) not null,
'Content' mediumtext,
'Photo' varchar (30) not null,
'Addtime' int (11) default NULL,
Primary key ('id ')
) ENGINE = MyISAM default charset = gbk AUTO_INCREMENT = 1 ";
$ SQL _create_table_reply = "CREATE TABLE 'reply '(
'Id' int (11) not null auto_increment,
'Msgid' int (11) not null,
'Content' mediumtext,
'Addtime' int (11) default NULL,
Primary key ('id ')
) ENGINE = MyISAM default charset = gbk AUTO_INCREMENT = 1 ";
If (mysql_query ($ SQL _drop_database, $ conn )){
If (mysql_query ($ SQL _create_database, $ conn )){
Mysql_select_db ($ database, $ conn );
If (mysql_query ($ SQL _create_table_messageboard, $ conn) & mysql_query ($ SQL _create_table_reply, $ conn )){
$ Config_file = "../config. php ";
$ Config_strings =" $ Config_strings. = "\ $ boardname = \" ". $ boardname." \ "; \ n ";
$ Config_strings. = "\ $ host = \" ". $ host." \ "; \ n ";
$ Config_strings. = "\ $ user = \" ". $ user." \ "; \ n ";
$ Config_strings. = "\ $ password = \" ". $ password." \ "; \ n ";
$ Config_strings. = "\ $ database = \" ". $ database." \ "; \ n ";
$ Config_strings. = "\ $ pagenum = \" ". $ pagenum." \ "; \ n ";
$ Config_strings. = "\ $ admin_name = \" ". $ admin_name." \ "; \ n ";
$ Config_strings. = "\ $ admin_password = \" ". $ admin_password." \ "; \ n ";
$ Config_strings. = "\ $ conn = mysql_connect (\ $ host, \ $ user, \ $ password); \ n ";
$ Config_strings. = "mysql_select_db (\ $ database, \ $ conn); \ n ";
$ Config_strings. = "?> ";
If ($ fp = fopen ($ config_file, "wb ")){
If (fwrite ($ fp, $ config_strings )){
If ($ fp2 = fopen ($ lockfile, 'w '))
{
Fwrite ($ fp2, '20140901 ');
Fclose ($ fp2 );
}
Echo "installation successful! The configuration file is config. php. you can manually modify the file ";
Echo "\ n go to the system homepage ";
} Else {
Exit ("file write failed ");
}
Fclose ($ fp );
}
} Else {
Exit ("The create table statement cannot be executed:". $ SQL _create_table );
}
} Else {
Exit ("The create database statement cannot be executed:". $ SQL _create_database );
}
} Else {
Exit ("The drop database statement cannot be executed:". $ SQL _drop_database );
}
} Else {
Exit ("failed to connect to the database. check the MySQL host name, user name, and password ");
}
}


The generated config. php file:

The code is as follows:


$ Boardname = "RUIZHINET message book ";
$ Host = "localhost ";
$ User = "root ";
$ Password = "";
$ Database = "ruizhi_messageboard ";
$ Pagenum = "10 ";
$ Admin_name = "admin ";
$ Admin_password = "admin ";
$ Conn = mysql_connect ($ host, $ user, $ password );
Mysql_select_db ($ database, $ conn );
?>


Index. php: Program display page

The code is as follows:


If (! $ _ GET ["page"]) {
$ Page = 1;
} Else {
$ Page = $ _ GET ["page"];
}
$ SQL = "SELECT id FROM messageboard ";
$ Result = mysql_query ($ SQL, $ conn );
$ Row_num = mysql_num_rows ($ result );
$ Page_count = ceil ($ row_num/$ pagenum );
Echo "a total of". $ row_num. "messages ";
Echo "divided into". $ page_count. "page display ";
Echo "current". $ page. "page ";
?>


If ($ row_num = 0 ){
Echo "no message ";
} Else {
?>















































$ Offset = ($ page-1) * $ pagenum;$ Sql2 = "SELECT * FROM messageboard order by id desc limit $ offset, $ pagenum ";$ Result2 = mysql_query ($ sql2, $ conn );While ($ row = mysql_fetch_array ($ result2 )){$ Offset ++;Echo" ";Echo" ";Echo" ";If ($ row ["photo"]! = "NONE "){Echo" ";}$ Sql3 = "SELECT * FROM reply WHERE msgid = $ row [id] order by id DESC ";$ Result3 = mysql_query ($ sql3, $ conn );While ($ row3 = mysql_fetch_array ($ result3 )){Echo" ";Echo" ";}}?> If ($ page_count> 1 ){$ Prev_page = $ page-1;$ Next_page = $ page + 1;If ($ page <= 1 ){Echo "page 1 | ";} Else {Echo "page 1 | ";}If ($ prev_page <1 ){Echo "previous page | ";} Else {Echo "previous page | ";}If ($ next_page> $ page_count ){Echo "next page | ";} Else {Echo "next page | ";}If ($ page >=$ page_count ){Echo "last page ";} Else {Echo "last page ";}}?>
";
Echo $ offset .".";
Echo base64_decode ($ row [title]). "-". base64_decode ($ row [author]). "[". date ("Y, m, d", $ row [addtime]). "]";
If (isset ($ _ SESSION ["admin_name"]) & $ _ SESSION ["admin_name"]! = ""){
Echo "delete | ";
Echo "reply ";
}
Echo"
". Base64_decode ($ row [" content "])."
Reply: ". date (" Y, m, d ", $ row3 [addtime]);
If (isset ($ _ SESSION ["admin_name"]) & $ _ SESSION ["admin_name"]! = ""){
Echo "delete ";
}
Echo"
". Base64_decode ($ row3 [content])."

}
?>
If (isset ($ _ SESSION ["admin_name"]) & $ _ SESSION ["admin_name"]! = ""){
?>

Exit Management


} Else {
?>

Login management


}
?>


Saveadd. php: save the message

The code is as follows:


If (! $ _ POST ["author"] |! $ _ POST ["content"])
{
Echo" \ N ";
Echo "you did not fill in the message name or content, 2 seconds back to the home page ";
Exit ();
} Else {
$ Imgflag = 0;
Function random ($ length)
{
$ Hash = 'IMG -';
$ Chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxy ';
$ Max = strlen ($ chars)-1;
Echo (double) microtime () * 1000000;
For ($ I = 0; $ I <$ length; $ I ++)
{
$ Hash. = $ chars [mt_rand (0, $ max)];
}
Return $ hash;
}
Function fileext ($ filename)
{
Return substr (strrchr ($ filename, '.'), 1 );
}
If ($ _ FILES ["upfile"] ["name"]! = ""){
$ Uploaddir = "upfile /";
$ Type = array ("jpg", "gif", "bmp", "jpeg", "png ");
If (! In_array (strtolower (fileext ($ _ FILES ['upfile'] ['name']), $ type ))
{
Echo" \ N ";
$ Text = implode (",", $ type );
Echo "you can only upload the following types of files:", $ text ,"
";
Exit ();
}
Else
{
$ Filename = explode (".", $ _ FILES ['upfile'] ['name']);
Do
{
$ Filename [0] = random (10 );
$ Randname = implode (".", $ filename );
$ Uploadfile = $ uploaddir. $ randname;
} While (file_exists ($ uploadfile ));
If (move_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name'], $ uploadfile )){
Echo "image uploaded ";
$ Imgflag = 1;
}
Else {
Echo "an error occurred while uploading the image! ";
$ Imgflag = 0;
}
}
}
$ Author = base64_encode ($ _ POST ["author"]);
$ Content = base64_encode (ereg_replace ("\ r \ n ","
", Htmlspecialchars ($ _ POST [" content "]);
$ Smiles = base64_encode ($ _ POST ["smiles"]);
If ($ _ POST ["title"]) {
$ Title = base64_encode ($ _ POST ["title"]);
} Else {
$ Title = base64_encode ("No title ");
}
$ Addtime = time ();
If ($ imgflag = 1 ){
$ Photo = base64_encode ($ randname );
} Else {
$ Photo = "NONE ";
}
// Save data
$ SQL = "INSERT INTO messageboard (author, title, smiles, content, photo, addtime) VALUES ('$ author',' $ title', '$ smiles ', '$ content',' $ photo ', $ addtime )";
Mysql_query ($ SQL, $ conn );
Echo" \ N ";
Echo "thank you for your message. return to the homepage in 2 seconds ";
}
?>
Savereply. php: save the Reply
If (isset ($ _ SESSION ["admin_name"]) & $ _ SESSION ["admin_name"]! = "")
{
If (! $ _ POST ["content"])
{
Echo" \ N ";
Echo "you did not fill in the reply content, 2 seconds back to the home page ";
Exit ();
} Else {
$ Msgid = $ _ POST ["msgid"];
$ Content = base64_encode (ereg_replace ("\ r \ n ","
", Htmlspecialchars ($ _ POST [" content "]);
$ Addtime = time ();
// Save data
$ SQL = "INSERT INTO reply (msgid, content, addtime) VALUES ($ msgid, '$ content', $ addtime )";
Mysql_query ($ SQL, $ conn );
Echo" \ N ";
Echo "thank you for your message. return to the homepage in 2 seconds ";
}
}
?>

The main code of ghost is install/index. php: the code on the program installation page is as follows :? Php if ($ _ GET ["acti...

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.