PHP implements the message board function with Mysql database, and mysql message board

Source: Internet
Author: User

PHP implements the message board function with Mysql database, and mysql message board

The following message board is displayed:


I recently read the basic PHP syntax and want to use these basic things to implement the message board, which is also a consolidation of the basic knowledge.

What is a message board? A carrier that can be used to record and display text information.

Now let's get to the point and talk about how to implement this message board!

After the user submits the message, the related content is saved to the server. When he wants to view the message, the background reads all the messages and displays them in the browser. Then, the user can see the message.

In this case, the backend needs a tool that is easy to read and write data. I chose the mysql database to help me complete these tasks.

I wrote three PHP files:

Conn. php connects to the database;

Addmsg. php reads message-related content from the page and saves it to the (Insert) database;

Listmsg. php reads the message content from the database and displays it on the page;

1. Prepare to create a database table structureThe following table structure is in phpMyAdmin:


Table creation syntax

SQL CREATE TABLE syntax CREATE TABLE name (column name 1 data type, column name 2 data type, column name 3 data type ,....)

2. Connect to the mysql database using php, and select one of the databases.Here I chose the bbs database (created before ps). Below I will introduce several php library functions to be used,

Copy codeThe Code is as follows:
① Mysql_connect ("localhost", "root ","")

Php connects to mysql. The parameters are the mysql address (localhost represents the local machine), user name, and password.

Returned value: if the connection fails, false is returned. If the connection fails, a connection identifier is returned.

Copy codeThe Code is as follows:
② Mysql_select_db ($ dbName, $ conn );

Mysql can have many databases, so you need to select one of them for the following operations.

Parameter: the first is the database name, and the second is the link identifier. You can put the return value in ① here, which indicates that I will use mysql in ①.

Returned value: "false" indicates that the connection fails. "true" indicates that the connection is successful.

Copy codeThe Code is as follows:
③ Mysql_query (query, connection)

Parameter: query indicates the statement you want mysql to execute.

Connection (optional) the SQL connection identifier is described above.

Returned value: mysql_query () returns a Resource Identifier only for SELECT, SHOW, EXPLAIN, or DESCRIBE statements. If the query execution is incorrect, FALSE is returned.

For other types of SQL statements, if mysql_query () is executed successfully, TRUE is returned. If an error occurs, FALSE is returned.

Summary of the returned value: If the function fails to be executed, false is returned. If the function is executed successfully, the resource identifier is returned. If it is a SELECT, SHOW, EXPLAIN, or DESCRIBE statement, the resource identifier is returned, other statements return true;

After talking about this, the context of the message board has come out.

Start the code below

Conn. php

<Span style = "font-family: Comic Sans MS; font-size: 14px;"> <? Php include ("head. php "); $ dbName =" bbs "; $ conn = @ mysql_connect (" localhost "," root "," ") or die (" Database Link error "); $ flag = mysql_select_db ($ dbName, $ conn); mysql_query ("set names 'gbk'"); // use GBK Chinese encoding; function toHtmlcode ($ content) {return $ content = str_replace ("\ n", "<br>", str_replace ("", "", $ content) ;}?> </Span>

The preceding toHtmlcode UDF replaces the carriage return (\ n) in the string with the line break in html. <br> Replace the space with the space () in html ()
One function is described as follows:

Syntax

Copy codeThe Code is as follows:
Str_replace (find, replace, string, count)

Parameters Description
Find Required. Specifies the value to be searched.
Replace Required. Required replacementFindValue.
String Required. Specifies the string to be searched.
Count Optional. A variable that counts the number of replicas.

Addmsg. php

<Span style = "font-family: Comic Sans MS; font-size: 14px;"> <? Php // reference the previously written Connection database file include ("conn. php "); if (@ $ _ POST ['submit ']) {$ SQL =" insert into message (id, user, title, content, lastdate )". "values ('', '$ _ POST [userName]', '$ _ POST [title]', '$ _ POST [content]', now ())"; mysql_query ($ SQL); echo "added successfully" ;}?> <SCRIPT language = javascript> function CheckPost () {if (myform. userName. value = "") {alert ("Enter the user name"); myform. user. focus (); return false;} if (myform. title. value. length <5) {alert ("the title cannot be less than 5 characters"); myform. title. focus (); return false;} if (myform. content. value = "") {alert ("message content is required"); myform. content. focus (); return false ;}}</SCRIPT> <form action = "addmsg. php "method =" post "name =" myform "onsubmit =" return CheckPost (); "> usage name: <input type = "text" size = "10" name = "userName"/> <br/> title: <input type = "text" name = "title"/> <br/> content: <textarea name = "content" cols = "60" rows = "9"> </textarea> <br/> <input type = "submit" name = "submit" value = "Submit a message"/> </form> </span>

Include is the introduction of conn. php, similar to include in C Language

The $ _ POST variable is an array used to collect the values in the form from method = "post, the key-value pairs issued by post are stored in the $ _ POST array $ _ POST ['submit '] to obtain the value of the key submit. If a submit is triggered, that is, when CheckPost returns true, will post the value. Obviously, $ _ POST ['submit '] is not empty. if it is not null, it is true. Then, execute the insert statement in if. Save the message content in the mysql database.

Listmsg. php

<Span style = "font-family: Comic Sans MS; font-size: 14px;"> <? Php include ("conn. php");?> <Table width = 500 border = "0" align = "center" cellpadding = "5" cellspacing = "1" bgcolor = "# add3ef"> <? Php $ SQL = "SELECT * FROM message order by lastdate desc"; $ query = mysql_query ($ SQL); while ($ row = mysql_fetch_array ($ query) {?> <Tr bgcolor = "# eff3ff"> <td> <B> <big> title: <? = $ Row ['title']?> </Big> <B/> <B> <sub> User: <? = $ Row ['user']?> </Sub> </B> </td> </tr> <tr bgColor = "# ffffff"> <td> content: <? = ToHtmlcode ($ row ['content'])?> </Td> </tr> <? Php }?> </Table> </span>

Mixing php and html code looks messy.

Php retrieves the message content from mysql and displays it on the page. I will display it in table here. The main code is above.

The above is the message board function of PHP and Mysql database shared by xiaobian. I hope it will help you!

Articles you may be interested in:
  • A paging text-based PHP message board source code
  • A simple PHP & MYSQL message board source code
  • Find a good AJAX-based message board source code (PHP and ASP) for download
  • Create simple PHP & MYSQL message board from classic
  • Simple Example of storing data in php xml message board xml
  • CRUD (add, delete, modify, and query) operations of the message board developed by php
  • Php simple message board and reply function implementation

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.