[PHP] PHP + MYSQL message board Creation

Source: Internet
Author: User

PYTHON learning has come to an end. Recently, I have been studying PHP and recently saw the creation of message boards. So I have compiled some of my experiences for your reference.

PS: the attachment has a code package for you to download.


System Environment: WAMP

IDE: NETBEANS

SQL tool: NAVICAT


Display Effect

:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140114/0912552S2-0.jpg "title =" 1.jpg" alt = "wKiom1LOaN_ylQA2AABIuPmS5fQ575.jpg" style = "float: none;"/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140114/0912553243-1.jpg "title =" 2.jpg" alt = "wKiom1LOaN_AA_VnAAGjGc5OYew734.jpg" style = "float: none;"/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140114/091255JO-2.jpg "title =" 3.jpg" alt = "wKioL1LOaNPCSbOxAABUGpgitw4886.jpg" style = "float: none;"/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140114/0912554962-3.jpg "title =" 4.jpg" alt = "wKiom1LOaN_x9jSQAAAbiSstFWU938.jpg" style = "float: none;"/>


1. Import MYSQL Data:

Import the following SQL statement into the database, assuming the database name is called bbs, Here ensure that the library and table fields are encoded in UTF-8 Mode

-------------------------------------------

Create table 'message '(

'Id' tinyint (1) not null auto_increment,

'User' varchar (25) not null,

'Title' varchar (50) not null,

'Content' tinytext not null,

'Lastdate' date not null,

Primary key ('id ')

) ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 1;

--------------------------------------------


2. Some PHP code

Add. php

------------------------------------------------

<?

// Load the conn. php file

Include ("conn. php ");

// Load the head. php file

Include ("head. php ");

// Submit the form to the database

If ($ _ POST ['submit ']) {

$ SQL = "insert into message (id, user, title, content, lastdate )".

"Values ('', '$ _ POST [user]', '$ _ POST [title]', '$ _ POST [content]', now ())";

Mysql_query ($ SQL );

Echo "<script language = \" javascript \ "> alert ('added successfully'); history. go (-1) </script> ";

}


?>

<! -- Use JS to restrict characters in form input -->

<SCRIPT language = javascript>

Function CheckPost (){

If (myform. user. value = ""){

Alert ("Enter the user name ");

Myform. user. focus ();

Return false;

}

If (myform. title. value. length <5 ){

Alert ("the title cannot be less than five characters ");

Myform. title. focus ();

Return false;

}

If (myform. content. value = ""){

Alert ("message content is required ");

Myform. content. focus ();

Return false;

}


}

</SCRIPT>

<! -- HTML form structure -->

<Form action = "add. php" method = "post" name = "myform" onsubmit = "return CheckPost ();">

User: <input type = "text" size = "10" name = "user"/> <br>

Title: <input type = "text" name = "title" value = "value" size = "40" maxlength = "40"/> <br>

Content: <textarea name = "content"> </textarea> <br/>


<Input type = 'submit 'name = 'submit' value = "Publish a message"/>


</Form>

------------------------------------------------


Head. php

-------------------------------------------------

<! -- HTML header navigation link -->

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">

<Title> </title>

<Link href = \ '# \' "/css.css" rel = "stylesheet" type = "text/css">

<B> <a href = \ '# \' "/a> | <a href =" list. php "> browse messages </a> | <a href =" login. php "> login </a> </B>

<Hr size = "1">

-------------------------------------------------


List. php

-------------------------------------------------

<?

// Load the conn. php file

Include ("conn. php ");

// Load the head. php file

Include ("head. php ");


// Set the paging value to 5

$ Pagesize = 5;

// Obtain the complete address path after the Domain Name

$ Url = $ _ SERVER ["REQUEST_URI"];

// Obtain the url array of the fixed key value [path] [query]

$ Url = parse_url ($ url );

// Print_r ($ url );

// Obtain the url's [path] path value

$ Url = $ url [path];


// Obtain the test table information

$ Numq = mysql_query ("select * from message ");

// Obtain the number of rows in the test table

$ Num = mysql_num_rows ($ numq );


// Determine whether the page parameter value can be obtained

If ($ _ GET [page]) {

// Pass the value to pageval

$ Pageval = $ _ GET [page];

// Calculate the page value for SQL statements.

$ Page = ($ pageval-1) * $ pagesize;

// Equivalent to $ page = $ page. ',' means to connect the page parameter value

$ Page. = ",";

}


// Determines whether the total number of database entries is greater than the page number.

If ($ num> $ pagesize ){

// Determine the value of the previous and next pages. If the value is smaller than 0, it is processed as 0.

If ($ pageval <= 1)

$ Pageval = 1;

// Display the page

Echo "$ num total ".

// (...). Used to connect the variable name

"<A href = $ url? Page = ". ($ pageval-1)."> previous page </a> <a href = $ url? Page = ". ($ pageval + 1)."> next page </a> ";

}


?>

<! -- HTML message list structure -->

<Table width = 500 border = "0" align = "center" cellpadding = "5" cellspacing = "1" bgcolor = "# add3ef">

<?

$ SQL = "select * from message order by id desc limit $ page $ pagesize ";

$ Query = mysql_query ($ SQL );

While ($ row = mysql_fetch_array ($ query )){

?>

<Tr bgcolor = "# eff3ff">

<Td> title: <? = $ Row [title]?> User: <? = $ Row [user]?> </Td>

</Tr>

<Tr bgColor = "# ffffff">

<Td> content: <? Echo htmtocode ($ row [content]);?> </Td>

<Tr bgColor = "# ffffff">

<Td> time: <? = $ Row [lastdate]?> </Td>

</Tr>

<?

}

?>

</Table>

-------------------------------------------------


Conn. php

--------------------------------------------------

<?

// Log on to the MYSQL database

$ Conn = @ mysql_connect ("localhost", "root", "") or die ("Database Link error ");

// Enter the BBS Library

Mysql_select_db ("bbs", $ conn );

// Transmit data streams using UTF-8 Chinese Encoding

Mysql_query ("set names 'utf8 '");

// Modify the space and carriage return encoding to HTML identifiable encoding.

Function htmtocode ($ content ){

$ Content = str_replace ("\ n", "<br>", str_replace ("", "& nbsp;", $ content ));

Return $ content;

}


?>

--------------------------------------------------


Login. php

---------------------------------------------------

<?

// Load the conn. php file

Include ("conn. php ");

// Determine whether to exit and mark the cookie as out. The logon page is returned.

If ($ _ GET [out]) {

Setcookie ("cookie", "out ");

Echo "<script language = \" javascript \ "> location. href = 'login. php'; </script> ";

}

// Submit a form to check whether the logon ID is admin, whether the Password Matches the MD5 value of 'php', and mark the cookie as OK

If ($ _ POST [id] = 'admin '){

$ Pw = md5 ($ _ POST [pw]);

If ($ pw = 'e1bfd762321e409cee4ac0b6e841963c '){

Setcookie ("cookie", "OK ");

// Refresh the logon page to enable the cookie flag value.

Echo "<script language = \" javascript \ "> location. href = 'login. php'; </script> ";

}

}

// Load the head. php file

Include ("head. php ");

// When the cookie flag value is not OK, the logon page is displayed. Otherwise, the exit page is displayed.

If ($ _ COOKIE ['cooker']! = 'OK '){

?>

<! -- Use JS to restrict logon characters -->

<SCRIPT language = javascript>

Function Checklogin (){

If (myform. id. value = ""){

Alert ("Enter your login name ");

Myform. id. focus ();

Return false;

}

If (myform. id. value! = "Admin "){

Alert ("User Name error ");

Myform. id. focus ();

Return false;

}

If (myform. pw. value = ""){

Alert ("the password cannot be blank ");

Myform. pw. focus ();

Return false;

}


}

}

</SCRIPT>

<! -- HTML logon page -->

<Form action = "" method = "post" name = "myform" onsubmit = "return Checklogin ();">

Username: <INPUT type = "text" name = "id"/> <br>

Password & nbsp; Code: <INPUT type = "password" name = "pw"/>

<Input type = "submit" name = "submit" value = "login">


</Form>


<?

} Else {

?>

<! -- HTML exit page -->

<A href = '? Out = login '> exit </a>

<?

}

?>

---------------------------------------------------


This article from "all the way to the North" blog, please be sure to keep this source http://showerlee.blog.51cto.com/2047005/1350339

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.