Php simple message board instance Program-PHP source code

Source: Internet
Author: User
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:

The Code is as follows:

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:

Title: User:
Content:

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.

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.