Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the simple php News release system

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is "how to understand the simple php press release system", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the simple php press release system.

Simple php Press release system tutorial (first edition) Lecture 1: build a database with phpmyadmin first set up a database called yayu. Create a data table under this database called news. Let's move on to a key point, that is, to create each field under the table news.

So, what is a field? Popular point, it is the general name of a kind of thing. For example, all press release times are represented by a noun (from my experience, fields created with phpmyadmin can be in Chinese, but habitually in English, computers are the best in America). We use "time" to express it. There can be a lot of "publish time" under the field time, so how to distinguish these times? this can query the content under other fields, for example, we set up the news title field as "title", and the content under the title field is not the same in common sense. So you can query the publication time through a title. In fact, we can make every content under title or time correspond to a number, that is, the field--

"id": the field "id" is the default preferred field. Content under other fields can be repeated, but this field is an Arabic numeral that increases from 1. When setting this field, set the primary key, index, unique, and auto-increment. This auto-increment means to add automatically. When any field adds content, the field automatically increases by 1, that is, any field corresponds to a unique id, such as 1, 2, 0 27.

Let's talk about the establishment of the news field.

1. Id: the meaning is the number of each news, it is unique, the type is tingint, this tingint type does not need to specify a length, the system defaults to 4; select auto-increment in "extra" and select the primary key.

2. Author: the meaning is the author (news publisher), set the type to varchar. When setting the length of this field, if you consider that the author is all Chinese, then 8 bytes is the upper limit (4 Chinese characters), but if you consider that the author may be a foreigner, 8 bytes is obviously too few, and there is the same problem for other fields. Here we set the length to 8.

3. Title: the meaning is the news title, the type is varchar, the length is 60, and the attribute is primany key.

4. Content: the meaning is the content of news, and the type is text. This type does not need to set the length.

5. Source: the meaning is the source of news, the type is varchar, and the length is 60.

6. Date: the meaning is the publication time, the type is datetime, the length does not need to be set, and the attribute is primany key.

The following additions are made to the following field types:

1. Date: time and date type. Time and date types also include the following:

⊕ datetime: 0000-00-0000: 00:00

⊕ date: 0000-00-00

⊕ timestamp: 00000000000000 (14 zeros, length depending on the display size)

⊕ time: 00:00:00

⊕ year: 0000

2. The conten t field represents the news content, and because its capacity may be very large, it uses the text type (up to 65535 bytes).

3. The title field is set to primany key. If no news item has the same publishing time, the date field can also be used as? Primany key, so that it will be more convenient to sort and retrieve news in the future.

4. Although the field of text type also belongs to a character type, its size cannot be specified. If you set the length, the system will prompt the SQL statement to make an error.

Now the news datasheet is set up.

Since news can't be added by everyone, only administrators can do it, so now let's create a datasheet users to store administrators.

1: id: type is tinyint, additional setting is auto-increment, primary key.

2: name: meaning is administrator name, type is varchar, length is 8, attribute is primany key.

3: password: meaning password, type varchar, length 32.

4: mail: also an email address with a type of varchar and a length of 30.

Now that the two databases are set up, let's go into the development of the news program.

Yayu original php News system course (first Edition) Lecture 2: the Foundation of News Program

I. the basis of connecting to the database

We must first add, remove, modify and delete news, that is, connect to the host, select the database, and send a request to the database. Otherwise, it's all on paper. Here are three important MySQL statements: mysql_pconnect () (connect to the host), mysql_select_db () (select a database), and mysql_query () (make a request to the database).

1. Mysql_pconnect ()

Its function is to connect to the host.

Syntax: mysql_pconnect ("host to log in", "user name at login", "password")

Such as mysql_pconnect ("localhost", "root", "")

Where "localhost" and "root" are the hostname and user name by default of phpmyadmin, and the password is empty.

The same function is mysql_connect (). The syntax is the same, except that the former opens a long-term connection and cannot be closed using the mysql_close () function, while the latter closes it in time with mysql_close () after use. For a website, the former is better than the latter. This reduces the burden on the MySQL server to handle connections and close connections.

When this function connects successfully, it returns a connection ID. Therefore, this function is generally written as follows:

$link= mysql_pconnect ()

Speaking of which, add a function mysql_close ()

The syntax is: mysql_close ("connection to be closed ID")

For example: mysql_close ($link)

Note: all () are strings, so there is no need to "" when there is a $symbol.

2. Mysql_select_db ()

Its function is to select a database as the current database. All the future operations are carried out in this database. This function returns a true if the execution is successful, otherwise it returns false.

The syntax is: mysql_select_db ("database name", "connected ID")

The second parameter can be omitted, which automatically finds and connects the last connection ID used.

In this program, we write this sentence as: mysql_select_db ("yayu", $link)

3. Mysql_query ()

Its function is to send a string of requests to the server.

The syntax is: mysql_query ("string asking questions", concatenating ID)

The first parameter is a complete MySQL statement, and the second parameter can be omitted. I usually omit it.

You should use mysql_select_db () to specify the database to use before using this function.

When the question string is update, insert or delete, the result returned by the function is true or false, indicating whether the query was successful or not. If the question string is a select statement, a result ID is returned, and if an error occurs in select, false is returned.

After understanding the above three important functions, we can give orders to the database. So what do we use to give orders? Let's take a look at four more MySQL statements.

Two. basic knowledge of issuing request statements to the database

They are: insert (insert data into the database), delete (delete data in the data table), select (retrieve data), update (update data).

1 insert (): inserts data into the database.

Syntax A: insert into Datasheet name (Field 1, Field 2,... ) values ("content of field 1d", "content of field 2"... )

Syntax B: insert into data table name set field 1 = "contents of field 1", field 2 = "contents of field 2", …...

For A, the field name can be omitted, but the contents of the following values section must be in the same order as the fields defined in phpmyadmin.

Take a look at the example below:

A: insert into news (title,date,author,source,content) values ($title,$date,$author,$source,$content)

Note: the above "$." Represents the content of the field to be added, when defined: $…... = content

B: insert into news set title=$title,author=$author

Note: if the content is a number, "" can be omitted.

2 delete (): delete the data in the data table

Syntax: delete from data table name where specified location limit number

If there is no where specified place, all data in the table is gone. The limit number can tell the server the maximum number of rows that can be deleted.

Example: delete from news where id=$id limit 1

3. Select (): retrieve data

Syntax: select field name 1, field 2,... From data Table name where location

If you want to list data for all columns in all records, you can use "*" to represent the field name.

Example: a: select id,author from news where id=$id

B: select * from news where id=$id

4 update (): update data

The syntax and insert are almost exactly the same.

Such as: update news set author=$author,title=$title where id=$id

It is worth noting that when using update statements, be careful to use where sentences, otherwise it may result in a large amount of data loss. For example:

Update news set author= "Bud Rain"

This operation will turn all the authors in the table into "sprouts".

To take another positive example, in the record administrator's table users, if a field is age, which is used to store the age of users, one year later, if they want to increase their age by one year, they can use the following statement:

Update users set age=age+1

Great, we have mastered most of the basics of the program now, and a small number of us will master it in the examples later.

The problem we face now is how to write the algorithm.

Yayu original php News system tutorial (first Edition) Lecture 3: the algorithm of News Program (1)-add News

one. Add news

To add news is to add new data to the database.

The whole algorithm is like this: the administrator fills in the content of the news in the form, including: title,author,source,content, the content of the other two fields (id,time) is completed by the server, of course, you have to write your own program, but not manually. After submitting the forms, add them to the database using the MySQL statement.

Here is a brief introduction to the tags and tags in the form.

The label is a single-line text box. Our commonly used attributes are: name, type. The name attribute specifies the name of the variable, which represents the content in this. The type attribute specifies the nature of the content in. If type=text, it is general text. If type=password, the contents of this "" will be displayed as black dots in the browser, so that when entering the content, it will not be inadvertently seen by others and cause data security problems.

The label is a multiline text box, and the common attribute is name.

In this program, this procedure is as follows:

The "author" in "name=" author "represents the content in"." Similarly, this "author" can also be "title" or something else, and it is worth noting that this "author" is different from the field author. I mention two very similar concepts here: the field author and the variable $author (the "author" above is actually $author because it represents the content in "). Although they have almost the same name, they are definitely not the same thing. Author is a field name in the data table news, which PHP uses to have limited access to the data in MySQL, which cannot be replaced by other characters in the program; while $author is a variable symbol set by the user in this program, and its value is obtained by the name attribute of the corresponding element in ". Since it's just a variable symbol, we can use any field, as long as it matches the name attribute of the corresponding element in the form. We chose to use the same character as the field name so that we don't have to bother to remember one more variable name.

Let's take a look at the contents of the label:

The content in this is used to get the contents of the content field, because there is too much content in this field, so you can only use this label.

When we fill out the content, all we have to do is submit it, so how is this process realized? Let's take a look at the following procedures:

Here type= "submit" / type= "reset" stands for submitting and rewriting news, respectively. The contents of the value property are displayed on this button. The meaning in name= "reset" / name= "submit" is the same as above.

The form element in HTML is specifically responsible for interacting with users as well. When a button of type submit is clicked, all elements in the form are submitted as variables to the file referred to by action for processing. The variable name is determined by the element's name attribute. In this program, the code is as follows:

In this place, we put the data processing program on the same page (action=addnews.php). In the method attribute, we order method=post, where post is the way to pass the value. Now let's discuss the following programs on the page designated by action:

$author=$_POST ["author"]

The $author here is the variable name defined by us, and the author is the name defined in the name attribute in the tag. The way values are passed as defined by POST for method in. All the data obtained through this value transfer method will be collected at the cost of $_ POST ["].

The complete program of this type is as follows:

If (@ $_ POST ["submit"])

{

$author=$_POST ["author"]

$department=$_POST ["department"]

$title=$_POST ["title"]

$content=parsecontent ($_ POST ["content"])

$date=date ("y-m-d Hpuri")

Mysql_query ("INSERT INTO news (title,date,author,department,content) VALUES ('$title','$date','$author','$department','$content')")

}

See other books for the use of the date () function. There are a total of five fields above, and there is another field, id, because we chose auto-increment in "extra", so when the above data is inserted into the database, id is automatically added by 1.

Of course, before this program, you must first connect to the database, and all the following programs that connect to the database are the same, and you must first connect to the database.

Yayu original php News system tutorial (first Edition) Lecture 3: algorithms of News programs (2)-display

II. Display news

After adding the news, you can let others watch the news.

The algorithm here is like this: first display the news title and other additional content (such as publication time) on the front page of the news, which can output all the news headlines in a loop. To view the content of a specific news, click the hyperlink of the news title to go to a new page to view the news.

Link the database before you start the program.

When there is a lot of news, we have to page the news, and we set up to display 10 news items on each page.

The specific paging procedures are as follows:

$respage = mysql_query ("SELECT COUNT (*) FROM news;"); / / $num is the total number of records in the database

While ($row = mysql_fetch_row ($respage))

{

$num = $row [0]

}

$recordnum = 10

$pages = ceil ($num/$recordnum); / / $recordnum is the number of records displayed per page, and $pages is the total number of pages

If (@ $_ GET ["page"]) / / get the parameter page in url

{

/ / $current is the current page, $pre is the previous page, $next is the next page, and $pre and $next are the values of the connection parameter page of the previous page and the next page

/ / if the parameter in url is 1, set the current page to 1 previous page, $pre is also one, and $next is 2

If ($_ GET ["page"] = = 1)

{

$current = 1

$pre = 1

$next = 2

} else {

/ / if the parameter in url is not one (not the first page), the value of the current page is the parameter obtained in url, $pre is the current page minus 1, and $next is plus 1

$current = $_ GET ["page"]

$pre = $current-1

$next = $current+1

}

} else {

/ / if there is no parameter page in url, the current page is set to $pre=1,$next=2

$current = 1

$pre ='1'

$next = 2

}

$now = ($current-1) * $recordnum

$echopage = ""

Echopage. = ". $pages." Page "

$echopage. = "first page, last page"

$echopage. = "previous page and next page"

$echopage. = "to nth"

For ($iSuppli.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report