I will show you the message board: 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 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 a message, the related content is stored in the service.
I will show you the message board: 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 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 a message, the related content is stored in the service.
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,
The 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.
The 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.
The 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
<? 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 ","
", Str_replace (" "," ", $ content) ;}?>
The preceding toHtmlcode UDF replaces the carriage return (\ n) in the string with the line break in html.
To replace spaces with spaces () in html ()
One function is described as follows:
Syntax
The 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
<? 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" ;}?>