The following is a simple message board example, including creating a mysql DATA table and connecting to the mysql database, querying the data table, and submitting and saving the data and deleting the data, this is actually the most basic message board function. The following is an example. The following is a simple message board example, including creating a mysql DATA table and connecting to the mysql database, querying the data table, and submitting and saving the data and deleting the data, this is actually the most basic message board function. The following is an example.
Script ec (2); script
First, the database SQL is as follows:
| The Code is as follows: |
|
|
-- Table structure 'message '--
Create table 'message' ('id' int (10) not null auto_increment, 'user' varchar (25) character set utf8 not null,
'Title' varchar (50) character set utf8 not null, 'content' tinytext character set utf8 not null, 'lastdate' date not null,
Primary key ('id') ENGINE = InnoDB default charset = gbk AUTO_INCREMENT = 11; |
The program code is as follows:
Create config. php with the following code:
| The Code is as follows: |
|
|
Mysql_select_db ("lyb", $ conn); mysql_query ("set names utf8");?> |
Create index. php with the following code:
Accept submitted data and save it to the data table
| The Code is as follows: |
|
|
If ($ _ POST ['submit '] &! Empty ($ _ POST ['title']) &! Empty ($ _ POST ['user']) &! Empty ($ _ POST ['content'])
{$ SQL = "insert into message (id, user, title, content, lastdate) values ('', '$ _ POST [user]', '$ _ POST [title]', '$ _ POST [content]', now ())";
Mysql_query ($ SQL); // echo "successful" ;}?> |
Query message records
| The Code is as follows: |
|
|
Principle Analysis
Let's take a look at the warehouse receiving. This is the most basic thing that has not been processed by any data judgment.
| The Code is as follows: |
|
|
If ($ _ POST ['submit '] &! Empty ($ _ POST ['title']) &! Empty ($ _ POST ['user']) &! Empty ($ _ POST ['content'])
{$ SQL = "insert into message (id, user, title, content, lastdate) values ('', '$ _ POST [user]', '$ _ POST [title]', '$ _ POST [content]', now ())";
Mysql_query ($ SQL); // echo "successful" ;}?> |
Judge whether the data submitted by the user is not empty
$ _ POST ['submit '] &! Empty ($ _ POST ['title'] &! Empty ($ _ POST ['user']) &! Empty ($ _ POST ['content)
Security Analysis, this is easy to implement SQL Injection for everyone, because no filtering is done, as long as you search for a piece of injection code on the Internet, the injection can be successful, data can be repeatedly and infinitely submitted
| The Code is as follows: |
|
|
{$ SQL = "insert into message (id, user, title, content, lastdate) values ('', '$ _ POST [user]', '$ _ POST [title]', '$ _ POST [content]', now ())"; |
This is stored in the database, but no security processing is performed. Generally, we do not advocate directly storing $ _ POST [user] into the database, yes. After processing, you need to transfer the variable to the database,
Query a database
| The Code is as follows: |
|
|
$ SQL = "select * from message order by id desc ";
$ Query = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ query )) |
These three statements of code implement database query, which is actually very simple. We only process the query and there are no technical processing tasks, for example, determine whether there are values or how many records are read at a time. |